zoukankan      html  css  js  c++  java
  • 剑指Offer44 扑克牌的顺子

     1 /*************************************************************************
     2     > File Name: 44_ContinuesCards.cpp
     3     > Author: Juntaran
     4     > Mail: JuntaranMail@gmail.com
     5     > Created Time: 2016年09月04日 星期日 19时55分44秒
     6  ************************************************************************/
     7  
     8 #include <stdio.h>
     9 #include <stdlib.h>
    10 
    11 int cmp(const void *a, const void *b)
    12 {
    13     return *(int*)a - *(int*)b;
    14 }
    15 
    16 bool isContinues(int* nums, int length)
    17 {
    18     if (nums==NULL || length!=5)
    19         return false;
    20     
    21     qsort(nums, length, sizeof(int), cmp);
    22     
    23     int ZeroNum = 0;
    24     int GapNum  = 0;
    25     
    26     for (int i = 0; i < length; ++i)
    27     {
    28         if (nums[i] == 0)
    29             ZeroNum ++;
    30         
    31         if (ZeroNum > 2)            // 大小王最多两个
    32             return false;
    33         
    34         if (i >= 1)
    35         {
    36             if (nums[i]!=0 && (nums[i]-nums[i-1])==0)
    37                 return false;        // 重复数字
    38             
    39             if (nums[i-1]!=0 && (nums[i]-nums[i-1])>1)
    40                 GapNum += nums[i]-nums[i-1] - 1;
    41         }
    42     }
    43     
    44     if (GapNum > ZeroNum)
    45         return false;
    46     
    47     return true;
    48 }
    49 
    50 int main()
    51 {
    52     int nums[] = {4, 5, 0, 2, 0};
    53     int length = 5;
    54     
    55     if (isContinues(nums, length))
    56         printf("True
    ");
    57     else
    58         printf("False
    ");
    59     return 0;
    60 }
  • 相关阅读:
    2021.Jan.11.Mon
    Samba
    nfs
    python数据类型(字典dictionary2)
    python数据类型(字典dictionary1)
    python数据类型(元祖tuple)
    python数据类型(列表List2)
    python数据类型(列表List1)
    python数据类型(字符串String2)
    python数据类型(数字Numbers)
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5840431.html
Copyright © 2011-2022 走看看