From d9e1de8d8994a50a6782c8e577b5ec013716a3b1 Mon Sep 17 00:00:00 2001 From: linyongji Date: Thu, 14 Jan 2021 10:33:11 +0800 Subject: [PATCH] no message --- .gitignore | 43 +++++ .../config/1_ContainsNearbyDuplicate.ini | 11 ++ .../include/1_ContainsNearbyDuplicate.h | 2 + .../include/2_ExcelSheetColumnTitle.h | 5 + .../src/1_ContainsNearbyDuplicate.cpp | 66 +++++++ .../Alogrithm/src/2_ExcelSheetColumnTitle.cpp | 20 ++ Alogrithm/Alogrithm/src/main.cpp | 6 + Alogrithm/UnitTest/UnitTest.cpp | 29 +++ Alogrithm/UnitTest/UnitTest.vcxproj | 173 ++++++++++++++++++ Alogrithm/UnitTest/UnitTest.vcxproj.filters | 30 +++ Alogrithm/UnitTest/UnitTest.vcxproj.user | 4 + Alogrithm/UnitTest/pch.cpp | 59 ++++++ Alogrithm/UnitTest/pch.h | 20 ++ 13 files changed, 468 insertions(+) create mode 100644 Alogrithm/Alogrithm/config/1_ContainsNearbyDuplicate.ini create mode 100644 Alogrithm/Alogrithm/include/1_ContainsNearbyDuplicate.h create mode 100644 Alogrithm/Alogrithm/include/2_ExcelSheetColumnTitle.h create mode 100644 Alogrithm/Alogrithm/src/1_ContainsNearbyDuplicate.cpp create mode 100644 Alogrithm/Alogrithm/src/2_ExcelSheetColumnTitle.cpp create mode 100644 Alogrithm/Alogrithm/src/main.cpp create mode 100644 Alogrithm/UnitTest/UnitTest.cpp create mode 100644 Alogrithm/UnitTest/UnitTest.vcxproj create mode 100644 Alogrithm/UnitTest/UnitTest.vcxproj.filters create mode 100644 Alogrithm/UnitTest/UnitTest.vcxproj.user create mode 100644 Alogrithm/UnitTest/pch.cpp create mode 100644 Alogrithm/UnitTest/pch.h diff --git a/.gitignore b/.gitignore index f4771ca..0224226 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,46 @@ Alogrithm/.vs/Alogrithm/v16/TestStore/1/testlog.manifest Alogrithm/.vs/Alogrithm/v16/TestStore/2/000.testlog Alogrithm/.vs/Alogrithm/v16/TestStore/2/testlog.manifest Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/c09df40e8d43ddea/MAIN.ipch +.vs/ProjectSettings.json +.vs/slnx.sqlite +.vs/VSWorkspaceState.json +Alogrithm/.vs/Alogrithm/v16/Solution.VC.db +Alogrithm/.vs/Alogrithm/v16/ipch/a4e78806f8e90de7.ipch +Alogrithm/.vs/Alogrithm/v16/ipch/a9bd1b0c07fd7d97.ipch +Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/68cf552fa8251aa6/2_EXCELSHEETCOLUMNTITLE.ipch +Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/907a8f897d64c1f1/1_CONTAINSNEARBYDUPLICATE.ipch +Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/ef33c62a547d2002/1_CONTAINSNEARBYDUPLICATE.ipch +Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/f25efc4d6f84971c/MAIN.ipch +Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/f97b2c42bb795e41/2_EXCELSHEETCOLUMNTITLE.ipch +Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/fb28aefeba7b8b20/1_CONTAINSNEARBYDUPLICATE.ipch +Alogrithm/.vs/Alogrithm/v16/TestStore/3/000.testlog +Alogrithm/.vs/Alogrithm/v16/TestStore/3/testlog.manifest +Alogrithm/Alogrithm/Debug/1_ContainsNearbyDuplicate.obj +Alogrithm/Alogrithm/Debug/2_ExcelSheetColumnTitle.obj +Alogrithm/Alogrithm/Debug/Alogrithm.Build.CppClean.log +Alogrithm/Alogrithm/Debug/Alogrithm.exe.recipe +Alogrithm/Alogrithm/Debug/Alogrithm.log +Alogrithm/Alogrithm/Debug/Alogrithm.vcxproj.FileListAbsolute.txt +Alogrithm/Alogrithm/Debug/main.obj +Alogrithm/Alogrithm/Debug/vc142.idb +Alogrithm/Alogrithm/Debug/vc142.pdb +Alogrithm/Alogrithm/Debug/Alogrithm.tlog/Alogrithm.lastbuildstate +Alogrithm/Alogrithm/Debug/Alogrithm.tlog/CL.command.1.tlog +Alogrithm/UnitTest/Debug/pch.obj +Alogrithm/UnitTest/Debug/UnitTest.log +Alogrithm/UnitTest/Debug/UnitTest.pch +Alogrithm/UnitTest/Debug/vc142.idb +Alogrithm/UnitTest/Debug/vc142.pdb +Alogrithm/UnitTest/Debug/UnitTest.tlog/CL.command.1.tlog +Alogrithm/UnitTest/Debug/UnitTest.tlog/CL.read.1.tlog +Alogrithm/UnitTest/Debug/UnitTest.tlog/CL.write.1.tlog +Alogrithm/UnitTest/Debug/UnitTest.tlog/UnitTest.lastbuildstate +Alogrithm/UnitTest/Debug/UnitTest.tlog/unsuccessfulbuild +Alogrithm/Alogrithm/Debug/Alogrithm.tlog/CL.read.1.tlog +Alogrithm/Alogrithm/Debug/Alogrithm.tlog/CL.write.1.tlog +Alogrithm/Alogrithm/Debug/Alogrithm.tlog/link.command.1.tlog +Alogrithm/Alogrithm/Debug/Alogrithm.tlog/link.read.1.tlog +Alogrithm/Alogrithm/Debug/Alogrithm.tlog/link.write.1.tlog +Alogrithm/Debug/Alogrithm.exe +Alogrithm/Debug/Alogrithm.ilk +Alogrithm/Debug/Alogrithm.pdb diff --git a/Alogrithm/Alogrithm/config/1_ContainsNearbyDuplicate.ini b/Alogrithm/Alogrithm/config/1_ContainsNearbyDuplicate.ini new file mode 100644 index 0000000..e8edb69 --- /dev/null +++ b/Alogrithm/Alogrithm/config/1_ContainsNearbyDuplicate.ini @@ -0,0 +1,11 @@ +[Test1] +nums=1,2,3,1 +nExpect=1 +[Test1] +nums=1,0,1,1 +nExpect=1 +[Test3] +nums=1,2,3,1,2,3 +nExpect=0 + + diff --git a/Alogrithm/Alogrithm/include/1_ContainsNearbyDuplicate.h b/Alogrithm/Alogrithm/include/1_ContainsNearbyDuplicate.h new file mode 100644 index 0000000..fa05a5a --- /dev/null +++ b/Alogrithm/Alogrithm/include/1_ContainsNearbyDuplicate.h @@ -0,0 +1,2 @@ +#pragma once +bool ContainsNearbyDuplicate(int* nums, int numsSize, int k); diff --git a/Alogrithm/Alogrithm/include/2_ExcelSheetColumnTitle.h b/Alogrithm/Alogrithm/include/2_ExcelSheetColumnTitle.h new file mode 100644 index 0000000..f0f9351 --- /dev/null +++ b/Alogrithm/Alogrithm/include/2_ExcelSheetColumnTitle.h @@ -0,0 +1,5 @@ +#pragma once +#include +#include + +char* ExcelSheetColumnTitle(int n); \ No newline at end of file diff --git a/Alogrithm/Alogrithm/src/1_ContainsNearbyDuplicate.cpp b/Alogrithm/Alogrithm/src/1_ContainsNearbyDuplicate.cpp new file mode 100644 index 0000000..d3c605b --- /dev/null +++ b/Alogrithm/Alogrithm/src/1_ContainsNearbyDuplicate.cpp @@ -0,0 +1,66 @@ +#include +#include +#include "../include/1_ContainsNearbyDuplicate.h" + +//解题思路:先排序,再查找,再判断 +typedef struct node_t { //定义结构体数组类型 + int value; //存放 数组值 + int index; //存放 下标 +}node; +int cmp(const void* a, const void* b) //qsort 里的cmp函数 +{ + if (((node*)a)->value < ((node*)b)->value) + return -1; + else if (((node*)a)->value == ((node*)b)->value) + return 0; + else + return 1; +} +int fun(int a, int b) //两数相减求绝对值 +{ + int t; + t = a - b; + if (t < 0) + t = t - 2 * t; + return t; +} + +bool ContainsNearbyDuplicate(int* nums, int numsSize, int k) +{ + node* arr = NULL; //定义一个node 指针 + int i = 0; + int j = 0; + arr = (node*)malloc(sizeof(node) * 100000);//为arr指针分配足够空间,用来保存数组 + if (NULL == arr) + { + printf("内存分配失败\n"); + } + else + { + for (i = 0; i < numsSize; i++)//遍历数组nums,把nums里的值和对应的下标保存在arr中 + { + arr[i].value = nums[i]; + arr[i].index = i; + } + qsort(arr, numsSize, sizeof(node), cmp);//调用c库函数qsort,对得到的数组arr进行排序 + for (i = 0; i < numsSize; i++)//对排好序的数组arr遍历,寻找返回true的条件 + { + for (j = i + 1; j < numsSize; j++) + { + if (arr[i].value == arr[j].value) //第i个元素和i后面的元素作比较,判断值是否相等 + { + if (fun(arr[j].index, arr[i].index) <= k)//调用fun,求得下标差的绝对值,和k作比较 + { + return true; + } + } + else {//不符合则跳出循环 + break; + } + } + } + } + free(arr);//释放arr + return false;//循环执完以后,说明没有符合的结果,则返回false; +} + diff --git a/Alogrithm/Alogrithm/src/2_ExcelSheetColumnTitle.cpp b/Alogrithm/Alogrithm/src/2_ExcelSheetColumnTitle.cpp new file mode 100644 index 0000000..877cfff --- /dev/null +++ b/Alogrithm/Alogrithm/src/2_ExcelSheetColumnTitle.cpp @@ -0,0 +1,20 @@ +#include "../include/2_ExcelSheetColumnTitle.h" +#define STR_LEN 100 +//给定一个整数,返回它在 Excel 表中相对应的列名称,如果在Excel表中找不到该列,则返回""。 +char* ExcelSheetColumnTitle(int n) +{ + char str[STR_LEN] = { 0 }; + int i = 0; + while (n > 0) { + if ((n > 0) && (n <= 26)) { + str[i] = 'A' + n - 1; + } + else if (n > 26) { + str[i] = 'A'; + } + n = n - 26; + i++; + } + printf("%s", str); + return str; +} \ No newline at end of file diff --git a/Alogrithm/Alogrithm/src/main.cpp b/Alogrithm/Alogrithm/src/main.cpp new file mode 100644 index 0000000..c26c671 --- /dev/null +++ b/Alogrithm/Alogrithm/src/main.cpp @@ -0,0 +1,6 @@ +#include "../include/2_ExcelSheetColumnTitle.h" +int main() +{ + ExcelSheetColumnTitle(10); + return 0; +} \ No newline at end of file diff --git a/Alogrithm/UnitTest/UnitTest.cpp b/Alogrithm/UnitTest/UnitTest.cpp new file mode 100644 index 0000000..d442764 --- /dev/null +++ b/Alogrithm/UnitTest/UnitTest.cpp @@ -0,0 +1,29 @@ +#include "pch.h" +#include "CppUnitTest.h" +#define FilePath "../Alogrithm/1_ContainsNearbyDuplicate.ini" +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace UnitTest +{ + TEST_CLASS(UnitTest) + { + public: + + + TEST_METHOD(TestMethode1) + { + char str[50][10] = { 0 };//保存ini文件里 节的名字 + int count = CalcCount(50, str);//保存ini文件里 节的个数 + bool nReal;//保存运算结果 + CString na, nb, nExpect; //b保存不同的键值 + for (int j = 0; j < count; j++) + { + GetPrivateProfileString(str[j], "na", " ", na.GetBuffer(20), 20, FilePath); + GetPrivateProfileString(str[j], "nExpect", " ", nExpect.GetBuffer(20), 20, FilePath); + nReal = add(_ttoi64(na), _ttoi64(nb)); //_ttoi是CString转为整型 + Assert::AreEqual(nReal, _ttoi64(nExpect)); + } + + } + }; +} diff --git a/Alogrithm/UnitTest/UnitTest.vcxproj b/Alogrithm/UnitTest/UnitTest.vcxproj new file mode 100644 index 0000000..976a2c6 --- /dev/null +++ b/Alogrithm/UnitTest/UnitTest.vcxproj @@ -0,0 +1,173 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {BABADA8A-F1CD-4EAA-9392-D0B01F8DF7A4} + Win32Proj + UnitTest + 10.0 + NativeUnitTestProject + + + + DynamicLibrary + true + v142 + MultiByte + false + + + DynamicLibrary + false + v142 + true + Unicode + false + + + DynamicLibrary + true + v142 + Unicode + false + + + DynamicLibrary + false + v142 + true + Unicode + false + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Use + Level3 + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + pch.h + + + Windows + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Use + Level3 + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + _DEBUG;%(PreprocessorDefinitions) + true + pch.h + + + Windows + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Use + Level3 + true + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Use + Level3 + true + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + NDEBUG;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Create + Create + Create + Create + + + + + + + + + + \ No newline at end of file diff --git a/Alogrithm/UnitTest/UnitTest.vcxproj.filters b/Alogrithm/UnitTest/UnitTest.vcxproj.filters new file mode 100644 index 0000000..bbfd7d9 --- /dev/null +++ b/Alogrithm/UnitTest/UnitTest.vcxproj.filters @@ -0,0 +1,30 @@ +锘 + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 婧愭枃浠 + + + 婧愭枃浠 + + + + + 澶存枃浠 + + + \ No newline at end of file diff --git a/Alogrithm/UnitTest/UnitTest.vcxproj.user b/Alogrithm/UnitTest/UnitTest.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/Alogrithm/UnitTest/UnitTest.vcxproj.user @@ -0,0 +1,4 @@ +锘 + + + \ No newline at end of file diff --git a/Alogrithm/UnitTest/pch.cpp b/Alogrithm/UnitTest/pch.cpp new file mode 100644 index 0000000..479152e --- /dev/null +++ b/Alogrithm/UnitTest/pch.cpp @@ -0,0 +1,59 @@ +锘// pch.cpp: 涓庨缂栬瘧鏍囧ご瀵瑰簲鐨勬簮鏂囦欢 + +#include "pch.h" +int CalcCount(int n, char(*str)[10]) +{ + TCHAR chSectionNames[2048] = { 0 };//瀹氫箟涓涓暟缁勶紝淇濆瓨璇诲彇鑺傜殑鍚嶅瓧 + char* pSectionName; //淇濆瓨鎵惧埌鐨勬煇涓妭鍚嶅瓧绗︿覆鐨勯鍦板潃 + int i = 0; //i鎸囧悜鏁扮粍chSectionNames鐨勬煇涓綅缃紝浠0寮濮嬶紝椤哄簭鍚庣Щ + int j = 0; //j鐢ㄦ潵淇濆瓨涓嬩竴涓妭鍚嶅瓧绗︿覆鐨勯鍦板潃鐩稿浜庡綋鍓峣鐨勪綅缃亸绉婚噺 + int k = 0; //涓存椂鍙橀噺锛岀敤浜庣粰str鏁扮粍璧嬪 + int m = 0; //涓存椂鍙橀噺锛岀敤浜庣粰str鏁扮粍璧嬪 + int count = 0; //缁熻鑺傜殑涓暟 + GetPrivateProfileSectionNames(chSectionNames, 2048, "..//ex1.ini");//鑾峰彇鏂囦欢涓殑鑺傚悕锛屾瘡涓妭鍚嶄互"\0"鍒嗗壊锛屼繚瀛樺湪chSectionNames涓 + //瑕佸姞澶存枃浠 + for (i = 0; i < 2048; i++, j++)//瀵筩hSectionName涓繚瀛樼殑鍐呭杩涜鎷嗗垎 + { + if (chSectionNames[0] == '\0')//濡傛灉绗竴涓瓧绗﹀氨鏄0锛屽垯璇存槑ini涓竴涓妭涔熸病鏈夛紝 + { + break; + } + if (chSectionNames[i] == '\0') + { + count++; + pSectionName = &chSectionNames[i - j]; //鎵惧埌涓涓0锛屽垯璇存槑浠庤繖涓瓧绗﹀線鍓嶏紝鍑忔帀j涓亸绉婚噺锛 + //灏辨槸涓涓妭鍚嶇殑棣栧湴鍧 + j = -1; //鎵惧埌涓涓妭鍚嶅悗锛宩鐨勫艰杩樺師锛屼互缁熻涓嬩竴涓妭鍚嶅湴鍧鐨勫亸绉婚噺 + //璧嬫垚-1鏄洜涓鸿妭鍚嶅瓧绗︿覆鐨勬渶鍚庝竴涓瓧绗0鏄粓姝㈢锛屼笉鑳戒綔涓鸿妭鍚 + for (m = 0; m <= strlen(pSectionName); m++)//鎶婃壘鍒扮殑鑺傚悕淇濆瓨鍒皊tr涓 + { + str[k][m] = *(pSectionName + m); + } + //str[k][++m] = '\0';//鎵嬪姩缁欒妭鍚嶅姞涓奬0,琛ㄧず杩欎釜鑺傚悕缁撴潫 + k++;//鍙橀噺鑷姞锛岀敤鏉ヤ繚瀛樹笅涓涓妭鍚 + //鍦ㄨ幏鍙栬妭鍚嶇殑鏃跺欏彲浠ヨ幏鍙栬鑺備腑閿殑鍊硷紝鍓嶆彁鏄垜浠煡閬撹鑺備腑鏈夊摢浜涢敭銆 + if (chSectionNames[i + 1] == 0) + { + break; //褰撲袱涓浉閭荤殑瀛楃閮芥槸0鏃讹紝鍒欐墍鏈夌殑鑺傚悕閮藉凡鎵惧埌锛屽惊鐜粓姝 + } + } + } + return count;//杩斿洖鑺傚悕鐨勪釜鏁 +} + +int* str_device(CString* str, int* value_count) +{ + char* token; //瀛樻斁琚垏鍓插悗鐨勭涓涓瓙涓 + static int Section_devide[500];//瀛樻斁瀛楃鍒囧壊瀹屾垚浠ュ悗鐨勬暟缁勫厓绱犲 + char Section_value[500] = { 0 };//瀛樻斁nums杞崲鎴恠tring绫诲瀷鐨勭粨鏋 + strcpy(Section_value, *str);//灏咰String绫诲瀷鐨勫瓧绗︿覆杞崲鎴恈har绫诲瀷锛屾柟渚垮悗闈㈠垏鍓插瓧绗︿覆 + //鑾峰緱鍒囧壊鍒扮殑绗竴涓瓧绗︿覆 + token = strtok(Section_value, ","); + /* 缁х画鑾峰彇鍏朵粬鐨勫瓙瀛楃涓 */ + while (token != NULL) { + Section_devide[*value_count] = (_ttoi)(token);//鎶婂垏鍓插緱鍒扮殑瀛愪覆杞负int锛屽瓨鍒版暟缁勪腑鍘汇 + token = strtok(NULL, ","); + (*value_count)++; //璁板綍瀛樹簡澶氬皯涓厓绱 + } + return Section_devide; //杩斿洖鏁扮粍棣栧湴鍧 +} \ No newline at end of file diff --git a/Alogrithm/UnitTest/pch.h b/Alogrithm/UnitTest/pch.h new file mode 100644 index 0000000..d1168e9 --- /dev/null +++ b/Alogrithm/UnitTest/pch.h @@ -0,0 +1,20 @@ +锘// pch.h: 杩欐槸棰勭紪璇戞爣澶存枃浠躲 +// 涓嬫柟鍒楀嚭鐨勬枃浠朵粎缂栬瘧涓娆★紝鎻愰珮浜嗗皢鏉ョ敓鎴愮殑鐢熸垚鎬ц兘銆 +// 杩欒繕灏嗗奖鍝 IntelliSense 鎬ц兘锛屽寘鎷唬鐮佸畬鎴愬拰璁稿浠g爜娴忚鍔熻兘銆 +// 浣嗘槸锛屽鏋滄澶勫垪鍑虹殑鏂囦欢涓殑浠讳綍涓涓湪鐢熸垚涔嬮棿鏈夋洿鏂帮紝瀹冧滑鍏ㄩ儴閮藉皢琚噸鏂扮紪璇戙 +// 璇峰嬁鍦ㄦ澶勬坊鍔犺棰戠箒鏇存柊鐨勬枃浠讹紝杩欏皢浣垮緱鎬ц兘浼樺娍鏃犳晥銆 + +#ifndef PCH_H +#define PCH_H + +#include +#include +#include +#include +#include + + +int CalcCount(int n, char(*str)[10]); +int* str_device(CString* str, int* value_count); + +#endif //PCH_H