标签:
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\n"); 57 else 58 printf("False\n"); 59 return 0; 60 }
标签:
原文地址:http://www.cnblogs.com/Juntaran/p/5840431.html