扑克牌顺子,扑克牌

  今天,?口交通大队民警在管区内免费向过路的城市居民发放交通安全扑克牌,希望能让城里人在打闹之中轻松学习交规。这种扑克牌画面内容由广泛道路交通标志、典型交通违法行为的卡通画、交通安全温馨指示语3部份组成,首批10000副交通安全扑克牌已六续发放到市民手中。by

题材:从扑克牌中随机抽5张牌,判断是还是不是七个顺子,即那五张牌是还是不是连连的。二-十为数字自身,A为1,J为1一,Q为12,K为壹三,而大小王能够看作任意数字。  

思路:把数组排序,计算数组中0的个数,总结排序之后的数组相邻数字之间的空缺总数。假设空缺的总额小于或许等于0的个数,那么那一个数组就是接2连3的;反之则不总是。最终,大家还需求专注的是,倘使数组中的非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 }

图片 1

标题:从扑克牌中随机抽5张牌,判断是还是不是多少个顺子,即那5张牌是否接二连3的。二-拾为数
字本人,A为一,J为1一,Q为1…

相关文章