Playing card shunzi code (c)
Address: http://blog.csdn.net/caroline_wendy
Question: Randomly select five cards from the playing cards to determine whether the cards are continuous or not.
2 ~ 10 is the number itself, a is 1, J is 11, Q is 12, K is 13, and the king of size can be seen as any number.
Sort to determine the number of intervals between strings. If the value is smaller than or equal to the number of kings, the value is continuous; otherwise, the value is not.
Code:
/* * main.cpp * * Created on: 2014.7.12 * Author: spike */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>using namespace std;int compare(const void* num1, const void* num2) {return *(int*)num1-*(int*)num2;}bool IsContinuous(int* numbers, int length) {if (numbers == NULL || length < 1)return false;qsort(numbers, length, sizeof(int), compare);int numberOfZero = 0;int numberOfGap = 0;for (int i=0; i<length&&numbers[i]==0; ++i)++numberOfZero;int small = numberOfZero;int big = small+1;while(big < length) {if (numbers[small] == numbers[big])return false;numberOfGap += numbers[big] - numbers[small]-1;small = big;++big;}return (numberOfGap>numberOfZero) ? false : true;}int main(void){ int numbers[] = {0, 3, 1, 4, 5}; bool result = IsContinuous(numbers, 5); printf("result = %s\n", result ? "true" : "false"); return 0;}
Output:
result = true