Playing cards, playing cards

Source: Internet
Author: User

Playing cards, playing cards
Question: randomly draw 5 cards from 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.

Idea: sort the array, count the number of zeros in the array, and count the number of vacancies between adjacent numbers in the sorted array. If the total number of vacancies is less than or equal to 0, this array is continuous; otherwise, it is not continuous. Finally, we need to note that if the non-zero number in the array repeats, the array is not continuous. Instead, the playing card description means that if a deck contains a pair, it cannot be a good match.

  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     else105         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     else117         printf("not Continuous.\n");118 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.