Huawei Machine Test-Calculate the number of mahjong (advanced 160 points)

Source: Internet
Author: User

A. The following topics



two. Topic Analysis

There are two forms of mahjong and cards, namely:

Mode 1:11,11,11,11,11,11,11      mode 2:11,123,123,123,123 (all or part 123 can be replaced by 111,1111)     Note: 123 = 3 consecutive cards in the same suit, such as 4d5d6d. 111=3 the same cards, such as 3t3t3t.    strip =t, tube =d

Mode 1 is called the "Qiao 7 pair", a group of cards is just 7 pairs of cards. But here is a question, "yes" can be repeated, that is, "bar" can be regarded as 2 "pair" (that is, "four in the title"). The normal and the hands of the final hand total of 14 cards, real life in the mode 1 and cards and calculate with "bar" double is impossible.

Mode 2 has many variants. First 123 refers to the continuous color of 3 cards, such as "1 2 strips 3" or "4 tube 5 Tube 6 tube" and so on. It can also be three kinds of the same suit, such as "1 tube 1 Tube 1 Tube or" 9 Article 9 Article 9 article ". If you have a group of cards, someone else has the same card or you receive the same card, this time it is "four to one" or "bar".

In daily life, in fact every "bar", you need to declare (whether it is the Ming "bar" or the Dark "bar"), and then take a card, that is, with each statement you have 1 bars, the last and the card when your cards are incremented by 1. Suppose such a scene, you have 13 cards in hand, which has a group of cards "2 2 bar 2", then you received a "2", then you have two options. (1) Declare that you received a "bar", that is, the dark bar, and then you take a card, if you and the card, then your cards have 13+1+1=15 Zhang, of course, this is not a clever 7 pair; If you do not, you have to play a card and then hit. (2) If you do not declare, and just formed a 7 pair, then you can and cards at this time a total of 14 cards, but this "2 2 article 2 Article 2" is not a "bar" because you do not declare that you can only use two pairs. If you do not have a card, then you have to play a card, that is, only 13 cards on the hand.
In general, in real life, you appear in the hands of "four to one", or declare, then you take a card, according to pattern 2 and cards. Either you do not declare, follow the pattern 1 and the card.

But the topic here does not specify, in strict consideration, mode 1 with "Four to one" and the card, that is, there is no declaration of "bar", I also calculated the number of times.

Examples of the method and number of cards in the program are as follows:

Mode 1:

 1t1t,2d2d,3t3t,4d4d,5d5d,6d6d,7t7t   .  Qiao 7 pairs: 2 1t1t, 2d2d,3t3t,4d4d,5d5d,6d6d6d6d.  Qiao 7 to +1 "four return one" = = 2+2=4 1t1t, 2d2d,3t3t3t3t, 5d5d,6d6d6d6d.  Qiao 7 to +2 "four return one" = = 2+4=6 1t1t, 2d2d2d2d,3t3t3t3t, 6d6d6d6d. Qiao 7 to +3 "four return one" = = 2+6=8 Fan

Mode 2:

1t1t,2d3d4d,4d5d6d,7d8d9d,2t3t4t.       0-fold 1t1t,2d3d4d,4d5d6d,7d8d9d,1t2t3t.     1-fold 1t1t,2d2d2d2d,4d5d6d,7d8d9d,2t3t4t.     2-fold 1t1t,2d2d2d2d,4d5d6d,7d8d9d,1t2t3t.     1 "Four return one" + card 2 ==2+1=3 1t1t,2d2d2d2d,5t5t5t,7d8d9d,1t2t3t. 1 "Four return to one" + card 2 ==2+1=3 fan

Mode 2, all or part of a series of three consecutive sets of cards such as "2t3t4t or" 7d8d9d "can be the same suit three cards such as" 5t5t5t "instead, as above. Even 4 consecutive cards of the same card namely "four to one" alternative, as below, of course the latter calculates the number.

1t1t,2d2d2d,5d5d5d5d,7d7d7d7d,5t6t7t.    2 "Four return one" ==4 1t1t,2d2d2d2d,5d5d5d5d,7d7d7d7d,1t2t3t.  3 "Four return one" + card 2 ==6+1=7 1t1t,2d2d2d2d,5d5d5d5d,7d7d7d7d,3t3t3t3t.    4 "Four return one" ==8  1t1t,2d2d2d2d,4d5d6d,1t2t3t,1t2t3t.        2+1+1=4 1t1t,2d2d2d2d,1t2t3t,1t2t3t,1t2t3t.    2+1+1+1=5 Fan

three. Program Ideas

Input:

Up to 18 cards, so the maximum storage space is 2*18+4 ', ' +1 '. + ' =42 '

to traverse a string:

1) Determine if there is a continuous three kinds of the same suit (KA), such as the shape of "4d5d6d". In this case, the Judgment (Ka2tiao) appears whether the card 2 is "1t2t3t".  2) judge (LIAN3) whether there are 3 consecutive cards of the same card, such as "7t7t7t"  3) to determine (gang) whether the "four-to-one" and count. 

Output:

1) If three consecutive cards 4d5d6d a group of cards appear, or three consecutive cards of the same card 7t7t7t a group of cards appear, or "Four to one" appeared 4 times, then the      card is in accordance with the pattern 2 and cards. number   = "Card 2" number of times * + "four to one" number of times *   2) Mode 1 is the number of 7 pairs and the number of cards     =2+2* "Four return one" 

Four. Program Code

#include <iostream> #include <string>using namespace Std;int main () {int fan;      int i;      int gang;     int Ka,lian3,ka2tiao;         Char s[42];  while (cin>>s) {Fan=gang=ka=lian3=ka2tiao=0;int len = strlen (s); for (i=0;i<len;i++) {if (s[i]<= ' 9 ' &&s[i]>= ' 1 ') {if (s[i]==s[i+2]&&s[i+2]==s[i+4]&& s[i+4]==s[i+6]&& (s[i+8]== ', ' | | s[i+8]== '. '))  Judge whether there is a bar and count, namely four return as 1d1d1d1dgang++; else if ((s[i]==s[0]| | s[i-1]== ', ') &&s[i]==s[i+2]&&s[i+2]==s[i+4]&& (s[i+6]== ', ' | | s[i+6]== '. '))  Determine if there is a 3, as 2d2d2d lian3=1; else if ((s[i]+2) = = (s[i+2]+1) && (s[i+2]+1) = = (S[i+4]) && (s[i+6]== ', ' | | s[i+6]== '. '))  Determine if there is a card, that is, NT (n+1) T (n+2) t {ka=1; if (s[i]== ' 1 ' &&s[i+1]== ' T ') ka2tiao++;//Judgment Card 2-----1t2t3t Number}}} if (ka| | lian3| |  GANG==4)//Existing NT (n+1) T (n+2) t or 2d2d2d or 4 "four-to-one" situation, must be the 2nd and the way of the card fan=ka2tiao*1+gang*2; Else fan=2+gang*2;//Clever seven pair of cards and the way cout<<fan<<endl;  } return 0;  }


Five. Test results



Huawei Machine Test-Calculate the number of mahjong (advanced 160 points)

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.