程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 撲克牌順子,撲克牌

撲克牌順子,撲克牌

編輯:C++入門知識

撲克牌順子,撲克牌


題目:從撲克牌中隨機抽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 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved