扑克牌顺子
Posted 三颗心
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了扑克牌顺子相关的知识,希望对你有一定的参考价值。
题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2-10为数字本身,A为1,J为11,Q为12,K为13,而大小王可以看成任意数字。
思路:把数组排序,统计数组中0的个数,统计排序之后的数组相邻数字之间的空缺总数。如果空缺的总数小于或者等于0的个数,那么这个数组就是连续的;反之则不连续。最后,我们还需要注意的是,如果数组中的非0数字重复出现,则该数组不是连续的。换成扑克牌的描述方式,就是如果一副牌里含有对子,则不可能是顺子。
1 #include "stdafx.h" 2 #include <stdlib.h> 3 4 int compare(const void* arg1, const void* arg2); 5 6 bool IsContinuous(int* numbers, int length) 7 { 8 if(numbers == NULL || length < 1) 9 return false; 10 11 qsort(numbers, length, sizeof(int), compare); 12 13 int numberOfZero = 0; 14 int numberOfGap = 0; 15 16 for(int i = 0 ; i < length && numbers[i] == 0 ; ++ i) 17 ++ numberOfZero; 18 19 int small = numberOfZero; 20 int big = small + 1; 21 22 while(big < length) 23 { 24 if(numbers[small] == numbers[big]) 25 return false; 26 27 numberOfGap += numbers[big] -numbers[small] - 1; 28 small = big; 29 30 ++ big; 31 } 32 33 return (numberOfGap > numberOfZero) ? false : true; 34 } 35 36 int compare(const void* arg1, const void * arg2) 37 { 38 return *(int*)arg1 - *(int*)arg2; 39 } 40 41 void Test(char* testName, int* numbers, int length, bool expected) 42 { 43 if(testName != NULL) 44 printf("%s begins: \\n", testName); 45 46 if(IsContinuous(numbers, length) == expected) 47 printf("Passed\\n"); 48 else 49 printf("Failed\\n"); 50 } 51 52 void Test1() 53 { 54 int numbers[] = {1,3,2,5,4}; 55 Test("Test1", numbers, sizeof(numbers) / sizeof(int), true); 56 } 57 58 void Test2() 59 { 60 int numbers[] = {1,3,2,6,4}; 61 Test("Test2", numbers, sizeof(numbers)/sizeof(int), false); 62 } 63 64 void Test3() 65 { 66 int numbers[] = {0,3,2,6,4}; 67 Test("Test3", numbers, sizeof(numbers)/ sizeof(int), true); 68 } 69 70 void Test4() 71 { 72 int numbers[] = {0,3,1,6,4}; 73 Test("Test4", numbers, sizeof(numbers) / sizeof(int), false); 74 } 75 76 void Test5() 77 { 78 int numbers[] = {1,3,0,5,0}; 79 Test("Test5", numbers, sizeof(numbers)/ sizeof(int), true); 80 } 81 82 int main() 83 { 84 int numbers[] = {1,3,2,5,4}; 85 int length = sizeof(numbers) / sizeof(int); 86 printf("the array number is:\\n"); 87 for(int i = 0 ; i < length; i ++) 88 printf("%d\\t", numbers[i]); 89 printf("\\n"); 90 if(IsContinuous(numbers, length)) 91 printf("is Continuous.\\n"); 92 else 93 printf("not Continuous.\\n"); 94 95 printf("\\n"); 96 int numbers1[] = {1,3,0,5,0}; 97 length = sizeof(numbers1) / sizeof(int); 98 printf("the array number is:\\n"); 99 for(int i = 0 ; i < length; i ++) 100 printf("%d\\t", numbers1[i]); 101 printf("\\n"); 102 if(IsContinuous(numbers1, length)) 103 printf("is Continuous.\\n"); 104 else 105 printf("not Continuous.\\n"); 106 107 printf("\\n"); 108 int numbers2[] = {1,3,2,6,4}; 109 length = sizeof(numbers2) / sizeof(int); 110 printf("the array number is:\\n"); 111 for(int i = 0 ; i < length; i ++) 112 printf("%d\\t", numbers2[i]); 113 printf("\\n"); 114 if(IsContinuous(numbers2, length)) 115 printf("is Continuous.\\n"); 116 else 117 printf("not Continuous.\\n"); 118 }
以上是关于扑克牌顺子的主要内容,如果未能解决你的问题,请参考以下文章
剑指 Offer 45. 把数组排成最小的数 剑指 Offer 61. 扑克牌中的顺子 剑指 Offer 40. 最小的k个数