[Post] playing card shunzi

Source: Internet
Author: User

From: http://zhedahht.blog.163.com/blog/static/25411174200951262930831/

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.

Analysis: this is a typical entertaining question. We need to abstract the background of playing cards into computer languages. We can think of five cards as an array consisting of five numbers. Mr. Wang is a special number and may treat them as 0, so that they will not be repeated with the numbers represented by other playing cards.

Next we will analyze how to judge whether the five numbers are continuous. The most intuitive way is to sort arrays. However, since 0 can be used as any number, we can use 0 to fill the vacancies in the array. That is to say, the sorted array is not consecutive, that is, the adjacent two numbers are separated by several numbers, but if we have enough 0, we can fill the gaps of the two numbers, this array is actually continuous. For example, the array is sorted by {0, 1, 3, 4, 5 }. There is a gap between 1 and 3, and we have a gap between 2, that is, we can use it as 2 to fill the gap.

So we need to do three things: 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.

Based on this idea, we can write the following code:

// Determine whether numbers in an array are continuous
// Parameters: numbers: an array, each number in the array is between
// 0 and maxNumber. 0 can be treeted as any number between
// 1 and maxNumber
// maxNumber: the maximum number in the array numbers
bool IsContinuous(std::vector<int> numbers, int maxNumber)
{
if(numbers.size() == 0 || maxNumber <=0)
return false;

// Sort the array numbers.
std::sort(numbers.begin(), numbers.end());

int numberOfZero = 0;
int numberOfGap = 0;

// how many 0s in the array?
std::vector<int>::iterator smallerNumber = numbers.begin();
while(smallerNumber != numbers.end() && *smallerNumber == 0)
{
numberOfZero++;
++smallerNumber;
}

// get the total gaps between all adjacent two numbers
std::vector<int>::iterator biggerNumber = smallerNumber + 1;
while(biggerNumber < numbers.end())
{
// if any non-zero number appears more than once in the array,
// the array can't be continuous
if(*biggerNumber == *smallerNumber)
return false;

numberOfGap += *biggerNumber - *smallerNumber - 1;
smallerNumber = biggerNumber;
++biggerNumber;
}

return (numberOfGap > numberOfZero) ? false : true;
}

In order to make the code more concise, the above Code uses the vector in the standard template library of C ++ to express the array and uses the function sort for sorting. Of course, we can write the Sorting Algorithm by ourselves. For better versatility, the above Code does not limit the length of the array and the maximum number allowed to appear. To answer the original question, we only need to ensure that the length of the input array is 5 and the maxnumber is 13.

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.