| Auteur | SHA1 | Message | Date |
|---|---|---|---|
|
|
b232fd355c | Merge branch '1' | il y a 4 ans |
|
|
e5f63bc0af | 分支1整理完成,提交完成的题目。 | il y a 4 ans |
| @@ -5,3 +5,17 @@ | |||
| *.testlog | |||
| *.manifest | |||
| *.user | |||
| *.obj | |||
| *.tlog | |||
| *.coverage | |||
| *.pdb | |||
| *.log | |||
| *.exe | |||
| *.lib | |||
| *.recipe | |||
| *.dll | |||
| *.idb | |||
| *.pch | |||
| *.txt | |||
| *.ilk | |||
| *.exp | |||
| @@ -17,7 +17,6 @@ | |||
| <Configuration>Release</Configuration> | |||
| <Platform>x64</Platform> | |||
| </ProjectConfiguration> | |||
| </ItemGroup> | |||
| <PropertyGroup Label="Globals"> | |||
| <VCProjectVersion>16.0</VCProjectVersion> | |||
| @@ -53,25 +52,23 @@ | |||
| <WholeProgramOptimization>true</WholeProgramOptimization> | |||
| <CharacterSet>Unicode</CharacterSet> | |||
| </PropertyGroup> | |||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | |||
| <ImportGroup Label="ExtensionSettings"> | |||
| </ImportGroup> | |||
| <ImportGroup Label="Shared" > | |||
| <ImportGroup Label="Shared"> | |||
| </ImportGroup> | |||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
| </ImportGroup> | |||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
| </ImportGroup> | |||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | |||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
| </ImportGroup> | |||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | |||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
| </ImportGroup> | |||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
| </ImportGroup> | |||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
| </ImportGroup> | |||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | |||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
| </ImportGroup> | |||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | |||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
| </ImportGroup> | |||
| <PropertyGroup Label="UserMacros" /> | |||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |||
| <LinkIncremental>true</LinkIncremental> | |||
| @@ -85,7 +82,6 @@ | |||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | |||
| <LinkIncremental>false</LinkIncremental> | |||
| </PropertyGroup> | |||
| <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |||
| <ClCompile> | |||
| <WarningLevel>Level3</WarningLevel> | |||
| @@ -142,9 +138,17 @@ | |||
| <GenerateDebugInformation>true</GenerateDebugInformation> | |||
| </Link> | |||
| </ItemDefinitionGroup> | |||
| <ItemGroup></ItemGroup> | |||
| <ItemGroup> | |||
| <ClInclude Include="include\1_ContainsNearbyDuplicate.h" /> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <ClCompile Include="src\1_ContainsNearbyDuplicate.cpp" /> | |||
| <ClCompile Include="src\main.cpp" /> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <None Include="config\1_ContainsNearbyDuplicate.ini" /> | |||
| </ItemGroup> | |||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | |||
| <ImportGroup Label="ExtensionTargets"> | |||
| </ImportGroup> | |||
| </Project> | |||
| </Project> | |||
| @@ -14,4 +14,22 @@ | |||
| <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | |||
| </Filter> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <ClInclude Include="include\1_ContainsNearbyDuplicate.h"> | |||
| <Filter>头文件</Filter> | |||
| </ClInclude> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <ClCompile Include="src\1_ContainsNearbyDuplicate.cpp"> | |||
| <Filter>源文件</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="src\main.cpp"> | |||
| <Filter>源文件</Filter> | |||
| </ClCompile> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <None Include="config\1_ContainsNearbyDuplicate.ini"> | |||
| <Filter>资源文件</Filter> | |||
| </None> | |||
| </ItemGroup> | |||
| </Project> | |||
| @@ -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 | |||
| @@ -0,0 +1,11 @@ | |||
| #pragma once | |||
| #include <stdio.h> | |||
| #include <stdlib.h> | |||
| 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); | |||
| @@ -0,0 +1,57 @@ | |||
| #include "../include/1_ContainsNearbyDuplicate.h" | |||
| //题目:给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j | |||
| // 使得 nums [i] = nums [j],并且i 和 j的差的绝对值至多为k。 | |||
| //思路:1.定义结构体数组,把数组每个元素的值及其对应下标存在结构体数组 | |||
| // 2.按照数组元素的值,对结构体数组排序 | |||
| // 3.遍历结构体数组,比较相结构体数组元素及其对应的下标,找到返回true,否则返回false | |||
| 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++){ //遍历数组nums,把nums里的值及其对应的下标保存在结构体数组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; | |||
| } | |||
| @@ -0,0 +1,8 @@ | |||
| #include <stdio.h> | |||
| int main() { | |||
| return 0; | |||
| } | |||
| @@ -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; | |||
| } | |||
| @@ -7,6 +7,19 @@ | |||
| #ifndef PCH_H | |||
| #define PCH_H | |||
| // 添加要在此处预编译的标头 | |||
| #include <stdio.h> | |||
| #include <stdlib.h> | |||
| #include <string.h> | |||
| #include<Windows.h> | |||
| #include <atlstr.h> | |||
| #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 | |||
| @@ -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) | |||
| { | |||
| } | |||
| }; | |||
| } | |||
| @@ -31,7 +31,7 @@ | |||
| <ConfigurationType>DynamicLibrary</ConfigurationType> | |||
| <UseDebugLibraries>true</UseDebugLibraries> | |||
| <PlatformToolset>v142</PlatformToolset> | |||
| <CharacterSet>Unicode</CharacterSet> | |||
| <CharacterSet>MultiByte</CharacterSet> | |||
| <UseOfMfc>false</UseOfMfc> | |||
| </PropertyGroup> | |||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | |||
| @@ -93,13 +93,15 @@ | |||
| <WarningLevel>Level3</WarningLevel> | |||
| <SDLCheck>true</SDLCheck> | |||
| <AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | |||
| <PreprocessorDefinitions>WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |||
| <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |||
| <UseFullPaths>true</UseFullPaths> | |||
| <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> | |||
| </ClCompile> | |||
| <Link> | |||
| <SubSystem>Windows</SubSystem> | |||
| <AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | |||
| <GenerateDebugInformation>DebugFull</GenerateDebugInformation> | |||
| <AdditionalDependencies>../code/Debug/*.obj;%(AdditionalDependencies)</AdditionalDependencies> | |||
| </Link> | |||
| </ItemDefinitionGroup> | |||
| <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | |||