JZ-C-44
剑指offer第四十四题:扑克牌的顺子:从扑克牌中随机抽5张牌,判断是不是顺子,大小鬼以0表示,可代替任何牌
1 //============================================================================ 2 // Name : JZ-C-44.cpp 3 // Author : Laughing_Lz 4 // Version : 5 // Copyright : All Right Reserved 6 // Description : 扑克牌的顺子:从扑克牌中随机抽5张牌,判断是不是顺子,大小鬼以0表示,可代替任何牌 7 //============================================================================ 8 9 #include <iostream> 10 #include <stdio.h> 11 #include <stdlib.h> 12 using namespace std; 13 14 int compare(const void *arg1, const void *arg2); 15 16 bool IsContinuous(int* numbers, int length) { 17 if (numbers == NULL || 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 if (numbers[small] == numbers[big]) 35 return false; 36 37 numberOfGap += numbers[big] - numbers[small] - 1;//+= 累加数字之间的空缺 38 small = big;//移动 39 ++big; 40 } 41 42 return (numberOfGap > numberOfZero) ? false : true; 43 } 44 45 int compare(const void *arg1, const void *arg2) { 46 return *(int*) arg1 - *(int*) arg2; 47 } 48 49 // ====================测试代码==================== 50 void Test(char* testName, int* numbers, int length, bool expected) { 51 if (testName != NULL) 52 printf("%s begins: ", testName); 53 54 if (IsContinuous(numbers, length) == expected) 55 printf("Passed.\n"); 56 else 57 printf("Failed.\n"); 58 } 59 60 void Test1() { 61 int numbers[] = { 1, 3, 2, 5, 4 }; 62 Test("Test1", numbers, sizeof(numbers) / sizeof(int), true); 63 } 64 65 void Test2() { 66 int numbers[] = { 1, 3, 2, 6, 4 }; 67 Test("Test2", numbers, sizeof(numbers) / sizeof(int), false); 68 } 69 70 void Test3() { 71 int numbers[] = { 0, 3, 2, 6, 4 }; 72 Test("Test3", numbers, sizeof(numbers) / sizeof(int), true); 73 } 74 75 void Test4() { 76 int numbers[] = { 0, 3, 1, 6, 4 }; 77 Test("Test4", numbers, sizeof(numbers) / sizeof(int), false); 78 } 79 80 void Test5() { 81 int numbers[] = { 1, 3, 0, 5, 0 }; 82 Test("Test5", numbers, sizeof(numbers) / sizeof(int), true); 83 } 84 85 void Test6() { 86 int numbers[] = { 1, 3, 0, 7, 0 }; 87 Test("Test6", numbers, sizeof(numbers) / sizeof(int), false); 88 } 89 90 void Test7() { 91 int numbers[] = { 1, 0, 0, 5, 0 }; 92 Test("Test7", numbers, sizeof(numbers) / sizeof(int), true); 93 } 94 95 void Test8() { 96 int numbers[] = { 1, 0, 0, 7, 0 }; 97 Test("Test8", numbers, sizeof(numbers) / sizeof(int), false); 98 } 99 100 void Test9() { 101 int numbers[] = { 3, 0, 0, 0, 0 }; 102 Test("Test9", numbers, sizeof(numbers) / sizeof(int), true); 103 } 104 105 void Test10() { 106 int numbers[] = { 0, 0, 0, 0, 0 }; 107 Test("Test10", numbers, sizeof(numbers) / sizeof(int), true); 108 } 109 110 // 有对子 111 void Test11() { 112 int numbers[] = { 1, 0, 0, 1, 0 }; 113 Test("Test11", numbers, sizeof(numbers) / sizeof(int), false); 114 } 115 116 // 鲁棒性测试 117 void Test12() { 118 Test("Test12", NULL, 0, false); 119 } 120 121 int main(int argc, char* argv) { 122 Test1(); 123 Test2(); 124 Test3(); 125 Test4(); 126 Test5(); 127 Test6(); 128 Test7(); 129 Test8(); 130 Test9(); 131 Test10(); 132 Test11(); 133 Test12(); 134 135 return 0; 136 }
—————————————————————————————————————行走在人猿的并行线——Laughing_Lz