zoukankan      html  css  js  c++  java
  • 扑克牌顺子

    题目:从扑克牌中随机抽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: 
    ", testName);
     45 
     46     if(IsContinuous(numbers, length) == expected)
     47         printf("Passed
    ");
     48     else
     49         printf("Failed
    ");
     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:
    ");
     87     for(int i = 0 ;  i < length; i ++)
     88         printf("%d	", numbers[i]);
     89     printf("
    ");
     90     if(IsContinuous(numbers, length))
     91         printf("is Continuous.
    ");
     92     else
     93         printf("not Continuous.
    ");
     94         
     95     printf("
    ");
     96     int numbers1[] = {1,3,0,5,0};
     97     length = sizeof(numbers1) / sizeof(int);
     98     printf("the array number is:
    ");
     99     for(int i = 0 ;  i < length; i ++)
    100         printf("%d	", numbers1[i]);
    101     printf("
    ");
    102     if(IsContinuous(numbers1, length))
    103         printf("is Continuous.
    ");
    104     else
    105         printf("not Continuous.
    ");
    106         
    107     printf("
    ");
    108     int numbers2[] = {1,3,2,6,4};
    109     length = sizeof(numbers2) / sizeof(int);
    110     printf("the array number is:
    ");
    111     for(int i = 0 ;  i < length; i ++)
    112         printf("%d	", numbers2[i]);
    113     printf("
    ");
    114     if(IsContinuous(numbers2, length))
    115         printf("is Continuous.
    ");
    116     else
    117         printf("not Continuous.
    ");
    118 }

  • 相关阅读:
    Windows server 2016 解决“无法完成域加入,原因是试图加入的域的SID与本计算机的SID相同。”
    Windows Server 2016 辅助域控制器搭建
    Windows Server 2016 主域控制器搭建
    Net Framework 4.7.2 覆盖 Net Framework 4.5 解决办法
    SQL SERVER 2012更改默认的端口号为1772
    Windows下彻底卸载删除SQL Serever2012
    在Windows Server2016中安装SQL Server2016
    SQL Server 创建索引
    C#控制台或应用程序中两个多个Main()方法的设置
    Icon cache rebuilding with Delphi(Delphi 清除Windows 图标缓存源代码)
  • 原文地址:https://www.cnblogs.com/sankexin/p/5641844.html
Copyright © 2011-2022 走看看