Programming Algorithm-playing card shunzi code (c)

Source: Internet
Author: User
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





Related Article

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.