diff --git a/15_NumberOf1InBinary/15_NumberOf1InBinary.vcxproj b/15_NumberOf1InBinary/15_NumberOf1InBinary.vcxproj new file mode 100644 index 0000000..3a37322 --- /dev/null +++ b/15_NumberOf1InBinary/15_NumberOf1InBinary.vcxproj @@ -0,0 +1,150 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {A9AF95C4-4FA9-4A7E-B005-005777651659} + Win32Proj + My15_NumberOf1InBinary + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/15_NumberOf1InBinary/15_NumberOf1InBinary.vcxproj.filters b/15_NumberOf1InBinary/15_NumberOf1InBinary.vcxproj.filters new file mode 100644 index 0000000..c3ba673 --- /dev/null +++ b/15_NumberOf1InBinary/15_NumberOf1InBinary.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;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 + + + + + Source Files + + + \ No newline at end of file diff --git a/15_NumberOf1InBinary/NumberOf1InBinary.cpp b/15_NumberOf1InBinary/NumberOf1InBinary.cpp new file mode 100644 index 0000000..d37c122 --- /dev/null +++ b/15_NumberOf1InBinary/NumberOf1InBinary.cpp @@ -0,0 +1,80 @@ +//================================================================== +// ¡¶½£Ö¸Offer¡ª¡ªÃûÆóÃæÊÔ¹Ù¾«½²µäÐͱà³ÌÌâ¡·´úÂë +// Öø×÷ȨËùÓÐÕߣººÎº£ÌÎ +//================================================================== + +// ÃæÊÔÌâ15£º¶þ½øÖÆÖÐ1µÄ¸öÊý +// ÌâÄ¿£ºÇëʵÏÖÒ»¸öº¯Êý£¬ÊäÈëÒ»¸öÕûÊý£¬Êä³ö¸ÃÊý¶þ½øÖƱíʾÖÐ1µÄ¸öÊý¡£ÀýÈç +// °Ñ9±íʾ³É¶þ½øÖÆÊÇ1001£¬ÓÐ2λÊÇ1¡£Òò´ËÈç¹ûÊäÈë9£¬¸Ãº¯ÊýÊä³ö2¡£ + +#include "cstdio" + +int NumberOf1_Solution1(int n) +{ + int count = 0; + unsigned int flag = 1; + while (flag) + { + if (n & flag) + count++; + + flag = flag << 1; + } + + return count; +} + +int NumberOf1_Solution2(int n) +{ + int count = 0; + + while (n) + { + ++count; + n = (n - 1) & n; + } + + return count; +} + +// ====================²âÊÔ´úÂë==================== +void Test(int number, unsigned int expected) +{ + int actual = NumberOf1_Solution1(number); + if (actual == expected) + printf("Solution1: Test for %p passed.\n", number); + else + printf("Solution1: Test for %p failed.\n", number); + + actual = NumberOf1_Solution2(number); + if (actual == expected) + printf("Solution2: Test for %p passed.\n", number); + else + printf("Solution2: Test for %p failed.\n", number); + + printf("\n"); +} + +int main(int argc, char* argv[]) +{ + // ÊäÈë0£¬ÆÚ´ýµÄÊä³öÊÇ0 + Test(0, 0); + + // ÊäÈë1£¬ÆÚ´ýµÄÊä³öÊÇ1 + Test(1, 1); + + // ÊäÈë10£¬ÆÚ´ýµÄÊä³öÊÇ2 + Test(10, 2); + + // ÊäÈë0x7FFFFFFF£¬ÆÚ´ýµÄÊä³öÊÇ31 + Test(0x7FFFFFFF, 31); + + // ÊäÈë0xFFFFFFFF£¨¸ºÊý£©£¬ÆÚ´ýµÄÊä³öÊÇ32 + Test(0xFFFFFFFF, 32); + + // ÊäÈë0x80000000£¨¸ºÊý£©£¬ÆÚ´ýµÄÊä³öÊÇ1 + Test(0x80000000, 1); + + return 0; +} + diff --git a/CodingInterviewChinese2.sln b/CodingInterviewChinese2.sln index bda7c5c..aad3b02 100644 --- a/CodingInterviewChinese2.sln +++ b/CodingInterviewChinese2.sln @@ -38,6 +38,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "13_RobotMove", "13_RobotMov EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "14_CuttingRope", "14_CuttingRope\14_CuttingRope.vcxproj", "{FF7ED3B0-5CE4-4D7B-954A-9EE93936D9A1}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "15_NumberOf1InBinary", "15_NumberOf1InBinary\15_NumberOf1InBinary.vcxproj", "{A9AF95C4-4FA9-4A7E-B005-005777651659}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -210,6 +212,16 @@ Global {FF7ED3B0-5CE4-4D7B-954A-9EE93936D9A1}.Release|x64.Build.0 = Release|x64 {FF7ED3B0-5CE4-4D7B-954A-9EE93936D9A1}.Release|x86.ActiveCfg = Release|Win32 {FF7ED3B0-5CE4-4D7B-954A-9EE93936D9A1}.Release|x86.Build.0 = Release|Win32 + {A9AF95C4-4FA9-4A7E-B005-005777651659}.Debug|Any CPU.ActiveCfg = Debug|Win32 + {A9AF95C4-4FA9-4A7E-B005-005777651659}.Debug|x64.ActiveCfg = Debug|x64 + {A9AF95C4-4FA9-4A7E-B005-005777651659}.Debug|x64.Build.0 = Debug|x64 + {A9AF95C4-4FA9-4A7E-B005-005777651659}.Debug|x86.ActiveCfg = Debug|Win32 + {A9AF95C4-4FA9-4A7E-B005-005777651659}.Debug|x86.Build.0 = Debug|Win32 + {A9AF95C4-4FA9-4A7E-B005-005777651659}.Release|Any CPU.ActiveCfg = Release|Win32 + {A9AF95C4-4FA9-4A7E-B005-005777651659}.Release|x64.ActiveCfg = Release|x64 + {A9AF95C4-4FA9-4A7E-B005-005777651659}.Release|x64.Build.0 = Release|x64 + {A9AF95C4-4FA9-4A7E-B005-005777651659}.Release|x86.ActiveCfg = Release|Win32 + {A9AF95C4-4FA9-4A7E-B005-005777651659}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE