Skip to content

Commit fedc01b

Browse files
committed
Initial commit for question 61
1 parent 6dafd9a commit fedc01b

File tree

4 files changed

+337
-0
lines changed

4 files changed

+337
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<ProjectGuid>{A82DDB97-1707-49EF-9828-6C83143DCDCB}</ProjectGuid>
23+
<Keyword>Win32Proj</Keyword>
24+
<RootNamespace>My61_ContinousCards</RootNamespace>
25+
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
26+
</PropertyGroup>
27+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
28+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
29+
<ConfigurationType>Application</ConfigurationType>
30+
<UseDebugLibraries>true</UseDebugLibraries>
31+
<PlatformToolset>v140</PlatformToolset>
32+
<CharacterSet>Unicode</CharacterSet>
33+
</PropertyGroup>
34+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
35+
<ConfigurationType>Application</ConfigurationType>
36+
<UseDebugLibraries>false</UseDebugLibraries>
37+
<PlatformToolset>v140</PlatformToolset>
38+
<WholeProgramOptimization>true</WholeProgramOptimization>
39+
<CharacterSet>Unicode</CharacterSet>
40+
</PropertyGroup>
41+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
42+
<ConfigurationType>Application</ConfigurationType>
43+
<UseDebugLibraries>true</UseDebugLibraries>
44+
<PlatformToolset>v140</PlatformToolset>
45+
<CharacterSet>Unicode</CharacterSet>
46+
</PropertyGroup>
47+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
48+
<ConfigurationType>Application</ConfigurationType>
49+
<UseDebugLibraries>false</UseDebugLibraries>
50+
<PlatformToolset>v140</PlatformToolset>
51+
<WholeProgramOptimization>true</WholeProgramOptimization>
52+
<CharacterSet>Unicode</CharacterSet>
53+
</PropertyGroup>
54+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
55+
<ImportGroup Label="ExtensionSettings">
56+
</ImportGroup>
57+
<ImportGroup Label="Shared">
58+
</ImportGroup>
59+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
60+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
61+
</ImportGroup>
62+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
63+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
64+
</ImportGroup>
65+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
66+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
67+
</ImportGroup>
68+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
69+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
70+
</ImportGroup>
71+
<PropertyGroup Label="UserMacros" />
72+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
73+
<LinkIncremental>true</LinkIncremental>
74+
</PropertyGroup>
75+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
76+
<LinkIncremental>true</LinkIncremental>
77+
</PropertyGroup>
78+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
79+
<LinkIncremental>false</LinkIncremental>
80+
</PropertyGroup>
81+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
82+
<LinkIncremental>false</LinkIncremental>
83+
</PropertyGroup>
84+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
85+
<ClCompile>
86+
<PrecompiledHeader>
87+
</PrecompiledHeader>
88+
<WarningLevel>Level3</WarningLevel>
89+
<Optimization>Disabled</Optimization>
90+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
91+
</ClCompile>
92+
<Link>
93+
<SubSystem>Console</SubSystem>
94+
<GenerateDebugInformation>true</GenerateDebugInformation>
95+
</Link>
96+
</ItemDefinitionGroup>
97+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
98+
<ClCompile>
99+
<PrecompiledHeader>
100+
</PrecompiledHeader>
101+
<WarningLevel>Level3</WarningLevel>
102+
<Optimization>Disabled</Optimization>
103+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
104+
</ClCompile>
105+
<Link>
106+
<SubSystem>Console</SubSystem>
107+
<GenerateDebugInformation>true</GenerateDebugInformation>
108+
</Link>
109+
</ItemDefinitionGroup>
110+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
111+
<ClCompile>
112+
<WarningLevel>Level3</WarningLevel>
113+
<PrecompiledHeader>
114+
</PrecompiledHeader>
115+
<Optimization>MaxSpeed</Optimization>
116+
<FunctionLevelLinking>true</FunctionLevelLinking>
117+
<IntrinsicFunctions>true</IntrinsicFunctions>
118+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
119+
</ClCompile>
120+
<Link>
121+
<SubSystem>Console</SubSystem>
122+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
123+
<OptimizeReferences>true</OptimizeReferences>
124+
<GenerateDebugInformation>true</GenerateDebugInformation>
125+
</Link>
126+
</ItemDefinitionGroup>
127+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
128+
<ClCompile>
129+
<WarningLevel>Level3</WarningLevel>
130+
<PrecompiledHeader>
131+
</PrecompiledHeader>
132+
<Optimization>MaxSpeed</Optimization>
133+
<FunctionLevelLinking>true</FunctionLevelLinking>
134+
<IntrinsicFunctions>true</IntrinsicFunctions>
135+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
136+
</ClCompile>
137+
<Link>
138+
<SubSystem>Console</SubSystem>
139+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
140+
<OptimizeReferences>true</OptimizeReferences>
141+
<GenerateDebugInformation>true</GenerateDebugInformation>
142+
</Link>
143+
</ItemDefinitionGroup>
144+
<ItemGroup>
145+
<ClCompile Include="ContinousCards.cpp" />
146+
</ItemGroup>
147+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
148+
<ImportGroup Label="ExtensionTargets">
149+
</ImportGroup>
150+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="ContinousCards.cpp">
19+
<Filter>Source Files</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
//==================================================================
2+
// 《剑指Offer——名企面试官精讲典型编程题》代码
3+
// 作者:何海涛
4+
//==================================================================
5+
6+
// 面试题61:扑克牌的顺子
7+
// 题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。
8+
// 2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王可以看成任意数字。
9+
10+
#include <cstdio>
11+
#include <cstdlib>
12+
13+
int Compare(const void *arg1, const void *arg2);
14+
15+
bool IsContinuous(int* numbers, int length)
16+
{
17+
if(numbers == nullptr || length < 1)
18+
return false;
19+
20+
qsort(numbers, length, sizeof(int), Compare);
21+
22+
int numberOfZero = 0;
23+
int numberOfGap = 0;
24+
25+
// 统计数组中0的个数
26+
for(int i = 0; i < length && numbers[i] == 0; ++i)
27+
++numberOfZero;
28+
29+
// 统计数组中的间隔数目
30+
int small = numberOfZero;
31+
int big = small + 1;
32+
while(big < length)
33+
{
34+
// 两个数相等,有对子,不可能是顺子
35+
if(numbers[small] == numbers[big])
36+
return false;
37+
38+
numberOfGap += numbers[big] - numbers[small] - 1;
39+
small = big;
40+
++big;
41+
}
42+
43+
return (numberOfGap > numberOfZero) ? false : true;
44+
}
45+
46+
int Compare(const void *arg1, const void *arg2)
47+
{
48+
return *(int*) arg1 - *(int*) arg2;
49+
}
50+
51+
// ====================测试代码====================
52+
void Test(const char* testName, int* numbers, int length, bool expected)
53+
{
54+
if(testName != nullptr)
55+
printf("%s begins: ", testName);
56+
57+
if(IsContinuous(numbers, length) == expected)
58+
printf("Passed.\n");
59+
else
60+
printf("Failed.\n");
61+
}
62+
63+
void Test1()
64+
{
65+
int numbers[] = { 1, 3, 2, 5, 4 };
66+
Test("Test1", numbers, sizeof(numbers) / sizeof(int), true);
67+
}
68+
69+
void Test2()
70+
{
71+
int numbers[] = { 1, 3, 2, 6, 4 };
72+
Test("Test2", numbers, sizeof(numbers) / sizeof(int), false);
73+
}
74+
75+
void Test3()
76+
{
77+
int numbers[] = { 0, 3, 2, 6, 4 };
78+
Test("Test3", numbers, sizeof(numbers) / sizeof(int), true);
79+
}
80+
81+
void Test4()
82+
{
83+
int numbers[] = { 0, 3, 1, 6, 4 };
84+
Test("Test4", numbers, sizeof(numbers) / sizeof(int), false);
85+
}
86+
87+
void Test5()
88+
{
89+
int numbers[] = { 1, 3, 0, 5, 0 };
90+
Test("Test5", numbers, sizeof(numbers) / sizeof(int), true);
91+
}
92+
93+
void Test6()
94+
{
95+
int numbers[] = { 1, 3, 0, 7, 0 };
96+
Test("Test6", numbers, sizeof(numbers) / sizeof(int), false);
97+
}
98+
99+
void Test7()
100+
{
101+
int numbers[] = { 1, 0, 0, 5, 0 };
102+
Test("Test7", numbers, sizeof(numbers) / sizeof(int), true);
103+
}
104+
105+
void Test8()
106+
{
107+
int numbers[] = { 1, 0, 0, 7, 0 };
108+
Test("Test8", numbers, sizeof(numbers) / sizeof(int), false);
109+
}
110+
111+
void Test9()
112+
{
113+
int numbers[] = { 3, 0, 0, 0, 0 };
114+
Test("Test9", numbers, sizeof(numbers) / sizeof(int), true);
115+
}
116+
117+
void Test10()
118+
{
119+
int numbers[] = { 0, 0, 0, 0, 0 };
120+
Test("Test10", numbers, sizeof(numbers) / sizeof(int), true);
121+
}
122+
123+
// 有对子
124+
void Test11()
125+
{
126+
int numbers[] = { 1, 0, 0, 1, 0 };
127+
Test("Test11", numbers, sizeof(numbers) / sizeof(int), false);
128+
}
129+
130+
// 鲁棒性测试
131+
void Test12()
132+
{
133+
Test("Test12", nullptr, 0, false);
134+
}
135+
136+
int main(int argc, char* argv[])
137+
{
138+
Test1();
139+
Test2();
140+
Test3();
141+
Test4();
142+
Test5();
143+
Test6();
144+
Test7();
145+
Test8();
146+
Test9();
147+
Test10();
148+
Test11();
149+
Test12();
150+
151+
return 0;
152+
}
153+

CodingInterviewChinese2.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "59_02_QueueWithMax", "59_02
152152
EndProject
153153
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "60_DicesProbability", "60_DicesProbability\60_DicesProbability.vcxproj", "{9810A644-A5F8-4AD4-BCEB-B756C94626BC}"
154154
EndProject
155+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "61_ContinousCards", "61_ContinousCards\61_ContinousCards.vcxproj", "{A82DDB97-1707-49EF-9828-6C83143DCDCB}"
156+
EndProject
155157
Global
156158
GlobalSection(SolutionConfigurationPlatforms) = preSolution
157159
Debug|Any CPU = Debug|Any CPU
@@ -894,6 +896,16 @@ Global
894896
{9810A644-A5F8-4AD4-BCEB-B756C94626BC}.Release|x64.Build.0 = Release|x64
895897
{9810A644-A5F8-4AD4-BCEB-B756C94626BC}.Release|x86.ActiveCfg = Release|Win32
896898
{9810A644-A5F8-4AD4-BCEB-B756C94626BC}.Release|x86.Build.0 = Release|Win32
899+
{A82DDB97-1707-49EF-9828-6C83143DCDCB}.Debug|Any CPU.ActiveCfg = Debug|Win32
900+
{A82DDB97-1707-49EF-9828-6C83143DCDCB}.Debug|x64.ActiveCfg = Debug|x64
901+
{A82DDB97-1707-49EF-9828-6C83143DCDCB}.Debug|x64.Build.0 = Debug|x64
902+
{A82DDB97-1707-49EF-9828-6C83143DCDCB}.Debug|x86.ActiveCfg = Debug|Win32
903+
{A82DDB97-1707-49EF-9828-6C83143DCDCB}.Debug|x86.Build.0 = Debug|Win32
904+
{A82DDB97-1707-49EF-9828-6C83143DCDCB}.Release|Any CPU.ActiveCfg = Release|Win32
905+
{A82DDB97-1707-49EF-9828-6C83143DCDCB}.Release|x64.ActiveCfg = Release|x64
906+
{A82DDB97-1707-49EF-9828-6C83143DCDCB}.Release|x64.Build.0 = Release|x64
907+
{A82DDB97-1707-49EF-9828-6C83143DCDCB}.Release|x86.ActiveCfg = Release|Win32
908+
{A82DDB97-1707-49EF-9828-6C83143DCDCB}.Release|x86.Build.0 = Release|Win32
897909
EndGlobalSection
898910
GlobalSection(SolutionProperties) = preSolution
899911
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)