diff --git a/.gitignore b/.gitignore
index 20d6ac6..c4154dd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,17 @@
*.testlog
*.manifest
*.user
+*.obj
+*.tlog
+*.coverage
+*.pdb
+*.log
+*.exe
+*.lib
+*.recipe
+*.dll
+*.idb
+*.pch
+*.txt
+*.ilk
+*.exp
diff --git a/.vs/Algorithm/v16/.suo b/.vs/Algorithm/v16/.suo
index 0c85e50..00d4a18 100644
Binary files a/.vs/Algorithm/v16/.suo and b/.vs/Algorithm/v16/.suo differ
diff --git a/code/code.vcxproj b/code/code.vcxproj
index 01d1d03..0f332f7 100644
--- a/code/code.vcxproj
+++ b/code/code.vcxproj
@@ -17,7 +17,6 @@
Release
x64
-
16.0
@@ -53,25 +52,23 @@
true
Unicode
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
true
@@ -85,7 +82,6 @@
false
-
Level3
@@ -142,9 +138,17 @@
true
-
-
+
+
+
+
+
+
+
+
+
+
-
+
\ No newline at end of file
diff --git a/code/code.vcxproj.filters b/code/code.vcxproj.filters
index afef69e..619842e 100644
--- a/code/code.vcxproj.filters
+++ b/code/code.vcxproj.filters
@@ -14,4 +14,22 @@
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/code/config/1_ContainsNearbyDuplicate.ini b/code/config/1_ContainsNearbyDuplicate.ini
new file mode 100644
index 0000000..c1961d1
--- /dev/null
+++ b/code/config/1_ContainsNearbyDuplicate.ini
@@ -0,0 +1,44 @@
+[Test1]
+array=1,2,3,1
+key=3
+output=1
+[Test2]
+array=1,0,1,1
+key=1
+output=1
+[Test3]
+array=1,2,3,1,2,3
+key=2
+output=0
+[Test4]
+array=1,2,3,1,2,3
+key=3
+output=1
+[Test5]
+array=1,0,3,4,2,1,0,3,4
+key=3
+output=0
+[Test6]
+array=8,7,15,1,6,1,9,15
+key=1
+output=0
+[Test7]
+array=8,7,15,1,6,1,9,15
+key=3
+output=1
+[Test8]
+array=1,4,3,3,2,2,3
+key=1
+output=1
+[Test9]
+array=1,8,3,9,1,5,6,9,5,2,7
+key=1
+output=0
+[Test10]
+array=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
+key=20
+output=0
+[Test11]
+array=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,1
+key=25
+output=1
\ No newline at end of file
diff --git a/code/include/1_ContainsNearbyDuplicate.h b/code/include/1_ContainsNearbyDuplicate.h
new file mode 100644
index 0000000..6097595
--- /dev/null
+++ b/code/include/1_ContainsNearbyDuplicate.h
@@ -0,0 +1,11 @@
+#pragma once
+#include
+#include
+
+typedef struct node_t { //ṹ
+ int value; // ֵ
+ int index; // ±
+}node;
+int Comper_1(const void* a, const void* b);
+int GetAbsolute(int a, int b);
+bool ContainsNearbyDuplicate(int* nums, int numsSize, int k);
diff --git a/code/src/1_ContainsNearbyDuplicate.cpp b/code/src/1_ContainsNearbyDuplicate.cpp
new file mode 100644
index 0000000..71ac474
--- /dev/null
+++ b/code/src/1_ContainsNearbyDuplicate.cpp
@@ -0,0 +1,57 @@
+#include "../include/1_ContainsNearbyDuplicate.h"
+//Ŀһһ kжǷͬ i j
+// ʹ nums [i] = nums [j]i jIJľֵΪk
+//˼·1.ṹ飬ÿԪصֵӦ±ڽṹ
+// 2.ԪصֵԽṹ
+// 3.ṹ飬ȽṹԪؼӦ±꣬ҵtruefalse
+
+
+bool ContainsNearbyDuplicate(int* nums, int numsSize, int k)
+{
+ int i = 0;
+ int j = 0;
+ node* arr = NULL;
+ arr = (node*)malloc(sizeof(node) * numsSize); //Ϊarrָ㹻ռ䣬
+ if (NULL == arr){
+ printf("ڴʧ\n");
+ }
+ else{
+ for (i = 0; i < numsSize; i++){ //numsnumsֵӦ±걣ڽṹarr
+ arr[i].value = nums[i];
+ arr[i].index = i;
+ }
+ qsort(arr, numsSize, sizeof(node), Comper_1); //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 (GetAbsolute(arr[j].index, arr[i].index) <= k){ //±ľֵkȽ
+ return true;
+ }
+ }
+ else { //ѭ
+ break;
+ }
+ }
+ }
+ }
+ free(arr);
+ return false; //ѭִԺ˵ûзϵĽfalse
+}
+
+int Comper_1(const void* a, const void* b) //qsortеıȽϺ
+{ //ֵ<0,ֵ>0,
+ if (((node*)a)->value < ((node*)b)->value)
+ return -1;
+ else if (((node*)a)->value == ((node*)b)->value)
+ return 0;
+ else
+ return 1;
+}
+int GetAbsolute(int a, int b) //ֵ
+{
+ int t;
+ t = a - b;
+ if (t < 0)
+ t = t - 2 * t;
+ return t;
+}
diff --git a/code/src/main.cpp b/code/src/main.cpp
new file mode 100644
index 0000000..6b7352f
--- /dev/null
+++ b/code/src/main.cpp
@@ -0,0 +1,8 @@
+#include
+
+int main() {
+
+
+
+ return 0;
+}
\ No newline at end of file
diff --git a/test/pch.cpp b/test/pch.cpp
index b6fb8f4..5417649 100644
--- a/test/pch.cpp
+++ b/test/pch.cpp
@@ -2,4 +2,71 @@
#include "pch.h"
-// 当使用预编译的头时,需要使用此源文件,编译才能成功。
+int CalcCount(int n, char(*str)[10], const char* FileName)
+{
+ TCHAR chSectionNames[2048] = { 0 };//定义一个数组,保存读取节的名字
+ char* pSectionName; //保存找到的某个节名字符串的首地址
+ int i = 0; //i指向数组chSectionNames的某个位置,从0开始,顺序后移
+ int j = 0; //j用来保存下一个节名字符串的首地址相对于当前i的位置偏移量
+ int k = 0; //临时变量,用于给str数组赋值
+ int m = 0; //临时变量,用于给str数组赋值
+ int count = 0; //统计节的个数
+ GetPrivateProfileSectionNames(chSectionNames, 2048, FileName);//获取文件中的节名,每个节名以"\0"分割,保存在chSectionNames中
+ //要加头文件
+ for (i = 0; i < 2048; i++, j++)//对chSectionName中保存的内容进行拆分
+ {
+ if (chSectionNames[0] == '\0')//如果第一个字符就是0,则说明ini中一个节也没有,
+ {
+ break;
+ }
+ if (chSectionNames[i] == '\0')
+ {
+ count++;
+ pSectionName = &chSectionNames[i - j]; //找到一个0,则说明从这个字符往前,减掉j个偏移量,
+ //就是一个节名的首地址
+ j = -1; //找到一个节名后,j的值要还原,以统计下一个节名地址的偏移量
+ //赋成-1是因为节名字符串的最后一个字符0是终止符,不能作为节名
+ for (m = 0; m <= strlen(pSectionName); m++)//把找到的节名保存到str中
+ {
+ str[k][m] = *(pSectionName + m);
+ }
+ k++;//变量自加,用来保存下一个节名
+ //在获取节名的时候可以获取该节中键的值,前提是我们知道该节中有哪些键。
+ if (chSectionNames[i + 1] == 0)
+ {
+ break; //当两个相邻的字符都是0时,则所有的节名都已找到,循环终止
+ }
+ }
+ }
+ return count;//返回节名的个数
+}
+
+//***************************************************************************************************//
+//************切割字符串函数,返回值为int数组的首地址,通过传入int指针带回数组元素个数***************//
+//***************************************************************************************************//
+int* str_device(CString str, int* value_count)
+{
+ char* token; //存放被切割后的第一个子串
+ int* Section_devide = (int*)malloc(sizeof(int) * 200);//存放字符切割完成以后的数组元素值
+ char Section_value[500] = { 0 };//存放nums转换成string类型的结果
+ memset(Section_value, 0, sizeof(char) * 500);
+ *value_count = 0;
+ strcpy(Section_value, str);//将CString类型的字符串转换成char类型,方便后面切割字符串
+ //获得切割到的第一个字符串
+ token = strtok(Section_value, ",");
+ /* 继续获取其他的子字符串 */
+ while (token != NULL) {
+ Section_devide[*value_count] = (_ttoi)(token);//把切割得到的子串转为int,存到数组中去。
+ token = strtok(NULL, ",");
+ (*value_count)++; //记录存了多少个元素
+ }
+ return Section_devide; //返回数组首地址
+}
+//字符串转bool
+bool CstrToBool(CString str)
+{
+ if (str == "1") {
+ return 1;
+ }
+ return 0;
+}
diff --git a/test/pch.h b/test/pch.h
index 69fcd40..d0edafb 100644
--- a/test/pch.h
+++ b/test/pch.h
@@ -7,6 +7,19 @@
#ifndef PCH_H
#define PCH_H
-// 添加要在此处预编译的标头
+#include
+#include
+#include
+#include
+#include
+
+
+#include"../code/include/1_ContainsNearbyDuplicate.h"
+
+
+int CalcCount(int n, char(*str)[10], const char* FileName);
+int* str_device(CString str, int* value_count);
+bool CstrToBool(CString str);
+
#endif //PCH_H
diff --git a/test/test.cpp b/test/test.cpp
index 96f9552..a5c62e4 100644
--- a/test/test.cpp
+++ b/test/test.cpp
@@ -2,15 +2,38 @@
#include "CppUnitTest.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
-
-namespace test
+#define FileName_1 "../code/config/1_ContainsNearbyDuplicate.ini"
+namespace test1
+{
+ TEST_CLASS(UnitTest_1)
+ {
+ TEST_METHOD(TestMethode1)
+ {
+ char Section_Name[100][10] = { 0 };
+ int Section_Count = CalcCount(100, Section_Name, FileName_1);
+ int array_count = 0;
+ int* Section_Value;
+ CString Na, Nb, nExpect;
+ for (int i = 0; i < Section_Count; i++) {
+ GetPrivateProfileString(Section_Name[i], "array", " ", Na.GetBuffer(200), 200, FileName_1);
+ GetPrivateProfileString(Section_Name[i], "key", " ", Nb.GetBuffer(20), 20, FileName_1);
+ GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(20), 20, FileName_1);
+ Section_Value = str_device(Na, &array_count);
+ bool nReal = ContainsNearbyDuplicate(Section_Value, array_count, _ttoi(Nb));
+ Assert::AreEqual(nReal, CstrToBool(nExpect));
+ }
+ }
+ };
+}
+namespace test2
{
TEST_CLASS(test)
{
public:
-
+
TEST_METHOD(TestMethod1)
{
}
};
}
+
diff --git a/test/test.vcxproj b/test/test.vcxproj
index c2403be..5fb2999 100644
--- a/test/test.vcxproj
+++ b/test/test.vcxproj
@@ -31,7 +31,7 @@
DynamicLibrary
true
v142
- Unicode
+ MultiByte
false
@@ -93,13 +93,15 @@
Level3
true
$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)
- WIN32;_DEBUG;%(PreprocessorDefinitions)
+ _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)
true
pch.h
Windows
$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)
+ DebugFull
+ ../code/Debug/*.obj;%(AdditionalDependencies)