PTA- Texas Hold'em solution to a problem

Texas Hold'em, huge quantities of a title question, is true with the project. However, the solution to a problem and more straightforward comments,It should be relatively easy to understand

Topics are as follows:

Recently, Akwa fans in Texas Hold'em. So she found a lot of people to play with her. Due to the large number of Akwa must change the rules of the game:

All cards are just look at numbers, excluding suit.
Each card is one kind 1,2,3,4,5,6,7,8,9,10,11,12,13 (corresponding A, 2,3,4,5,6,7 , 8,9,10, J, Q, K )
each player out of five cards from a complete poker (not king size), the value of hands that may arise from low to high are arranged as follows:

High card: do not include the following card poker. For all the high-card poker, in accordance with the value of five cards and were descending order.
Pair: 5 cards in the hands of the cards have the same value of 2. For cards have pairs, according to the value of the cards constituting the pair are sorted from largest to smallest. If these are the same, the value of the hand press in the remaining three cards and were descending order.
Two pairs: two different pairs of hands have pairs. For the hand contains two pairs, were descending order according to their highest value pairs of. If the same highest pair, the value of another press sub-sort in descending manner. If these values are identical, the value of the remaining press card sort descending manner.
Three: the hands have the same brand value of three. For all the hand contains three were descending sort by brand value of 23 of the constitution. If these values are identical, the value of the remaining press card sort descending manner.
Full house: the hands have a three and a pair. Similarly, press three in descending order, if three of the same size, the size of the sub-press sort.
24: The hands have the same brand value of four. For all the hand contains four were descending sort by brand value of 24 of the constitution. If these values are identical, the value of the remaining press card sort descending manner.
Junko: the hands have five cards of consecutive values. For the hand contains a straight sort by highest card straight.
Royal Flush: hands with 10 to A (10, J, Q, K, A). It is the largest hand!

Now, Akwa already know everyone's hand, she wants to know all of the ranking list. If the player's hand equal size, press lexicographical output of the player's name. To ensure there are no duplicate names. Can you help her?

Input formats:

The first line contains a positive integer N (1 <= N <= 100000), indicates the number of players.

Next N rows, each row comprising two strings: m (1 <= | m | <= 10), indicates the player's name; s (1 <= | s | <= 10), represents a player's hand.

Output formats:

N output ranked list of players.

Sample input:

3
Alice AAA109
Bob 678910
Good 678910

Sample output:

Good
Bob
Alice

solution:

head File

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

This problem comprises a structure struct People, three functions: ①int trans (string str) ②void deal_the_level_of (People x, int cur) ③int cmp (People a, People b), the main function:

struct People
{
    string name;
    string s;
    int card[14];
    double level;//大等级从1-8  细分等级顺序用小数点来算大小  
}f[100005];

Members of the structure is simple: name and hand are the string class, easy input and output split; card arrays are stored in this case owned brand; the last one of the most important grade, I used double type to define, priority is determined by the decimal card size; also define a global variable array input and facilitate subsequent processing.

int trans(string str)
{
    if(str=="A")return 1;
    if(str=="2")return 2;
    if(str=="3")return 3;
    if(str=="4")return 4;
    if(str=="5")return 5;
    if(str=="6")return 6;
    if(str=="7")return 7;
    if(str=="8")return 8;
    if(str=="9")return 9;
    if(str=="10")return 10;
    if(str=="J")return 11;
    if(str=="Q")return 12;
    if(str=="K")return 13;
}

Trans functions presumably everyone is familiar function, before programming the job many times as needed vernacular into a string of numbers has a value, this function is a string into a digital string class, i.e. corresponding to the sign face.

int cmp(People a,People b)
{
    if(a.level!=b.level)return a.level>b.level;
    else return a.name<b.name;
} 

The cmp (compare) function is to use the back sort function as its third argument, it means telling sort how to sort. This question is descending in accordance with the sign face to row, if the sign face on the same order according to the name of the dictionary to row. Since the problem with this is that the string type, for lexicographically sorting is very simple, a number less than "<" to get.

The following are the most important function of this question! ! Since this function is very long, there are more than 70 rows, it will be interpreted into the comments.

Function name suggests --deal the level of (a People type variable): treatment Level People variable x.

void deal_the_level_of(People x,int cur)    //current 当前的那个下标 
{
    int single_value=0;//剩下单张牌总值的和 
    int couple1=0,couple2=0;//有一个对子 、有两个对子 其值为对子对应的牌面 
    int three=0; //判断是否有三条 
    int four=0; //判断是否有四条 
    bool line=false;//判断是否有顺子 有的话更为true 
    int maxn;//顺子中最大的牌 

    for(int i=1;i<=13;i++)
    {
        if(x.card[i]==1)//遇到这张牌是单张的 
        {
            int judge=1,j;//判断顺子 
            for(j=i+1;j<=i+4;j++)judge*=x.card[j];//不断相乘,只有全都是 1 ,乘出来才会为1. 
            if(judge==1){//是顺子 
                line=true;
                maxn=j-1;
                break;
            }
            else single_value+=i;
        }
        if(x.card[i]==2)//如果这张牌拥有两张 
        {
            if(couple1==0)couple1=i;//如果之前没出现过对子 
            else couple2=i;//之前出现过对子了,那这个就是第二个对子 
        }
        if(x.card[i]==3)three=i;//如果这张牌拥有三张 
        if(x.card[i]>=4)//这张牌拥有四张或五张,一样的都归为 四条  这个级别。 
        {
            four=i;
            if(x.card[i]==5)single_value+=i;//五张一样的 剩下那张归为单张 
        }
    }
/*
注意!如果一个大等级中,有多个排序的优先级
那么:
第一优先级 乘 0.01
第二优先级    0.0001
第三优先级    0.000001
因为最大的单张牌面“K”的值为13,超过10了,如果少乘一个0会影响到大等级。 
*/ 
    if(x.card[10]*x.card[11]*x.card[12]*x.card[13]*x.card[1]==1)x.level+=8.0;//皇家同花顺 ,永远滴神! 
    else if(line)//顺子  第7级 
    {
        x.level=7.0;
        x.level+=maxn*0.01;
    }
    else if(four)//四条 第6级 
    {
        x.level=6.0;
        x.level+=four*0.01+single_value*0.0001;
    }
    else if(three)//满堂红或三条 
    {
        if(couple1){//有一个对子,满堂红  第5级 
            x.level=5.0;
            x.level+=three*0.01+couple1*0.0001; 
        }
        else{//三条 第4级 
            x.level=4.0; 
            x.level+=three*0.01+single_value*0.0001;
        }
    }
    else if(couple1)//两对或对子 
    {
        if(couple2){//两对  第3级 
            x.level=3.0;
            x.level+=couple2*0.01+couple1*0.0001+single_value*0.000001;
        }
        else{//单个对子  第2级 
            x.level=2.0;
            x.level+=couple1*0.01+single_value*0.0001;
        }
    }
    else {//高牌  最弱的第1级 
        x.level=1.0;
        x.level+=single_value*0.0001;
    }
    f[cur]=x;//不用指针,将其转移给当前的这个f (因为f数组是全局变量) ,不然f数组的值不会改变的 
}

The main function:

Explain or put it in comments!

int main()
{
    int n,value;
    string tmp;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>f[i].name>>f[i].s;
        string str=f[i].s;
        for(int j=0,t=0;t<5;t++)//共五张牌 
        {
            if(str[j]!='1'){ //不为10 
                tmp=str[j];  //提取出这个字符 
                value=trans(tmp);//转换这个字符为对应的值 
                f[i].card[value]++;//该值的手牌数+1 
                j++;
            }
            else{   //遇到10了,只有 10 才会出现 1这个数字,因为牌值为1的是 A 
                f[i].card[10]++;
                j+=2;   //跳两格           
            }
        }
        deal_the_level_of(f[i],i);//处理f[i],记得带下标i 
    }
    sort(f,f+n,cmp);    //排序 
    for(int i=0;i<n;i++)cout<<f[i].name<<endl;  //输出 
    return 0;
}

Complete code:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct People
{
    string name;
    string s;
    int card[14];
    double level;//大等级从1-8  细分等级顺序用小数点来算大小  
}f[100005];
int trans(string str)
{
    if(str=="A")return 1;
    if(str=="2")return 2;
    if(str=="3")return 3;
    if(str=="4")return 4;
    if(str=="5")return 5;
    if(str=="6")return 6;
    if(str=="7")return 7;
    if(str=="8")return 8;
    if(str=="9")return 9;
    if(str=="10")return 10;
    if(str=="J")return 11;
    if(str=="Q")return 12;
    if(str=="K")return 13;
}
int cmp(People a,People b)
{
    if(a.level!=b.level)return a.level>b.level;
    else return a.name<b.name;
} 
void deal_the_level_of(People x,int cur)//current 当前的那个下标 
{
    int single_value=0;//剩下单张牌总值的和 
    int couple1=0,couple2=0;//有一个对子 、有两个对子 其值为对子对应的牌面 
    int three=0; //判断是否有三条 
    int four=0; //判断是否有四条 
    bool line=false;//判断是否有顺子 有的话更为true 
    int maxn;//顺子中最大的牌 

    for(int i=1;i<=13;i++)
    {
        if(x.card[i]==1)//遇到这张牌是单张的 
        {
            int judge=1,j;//判断顺子 
            for(j=i+1;j<=i+4;j++)judge*=x.card[j];//不断相乘,只有全都是 1 ,乘出来才会为1. 
            if(judge==1){//是顺子 
                line=true;
                maxn=j-1;
                break;
            }
            else single_value+=i;
        }
        if(x.card[i]==2)//如果这张牌拥有两张 
        {
            if(couple1==0)couple1=i;//如果之前没出现过对子 
            else couple2=i;//之前出现过对子了,那这个就是第二个对子 
        }
        if(x.card[i]==3)three=i;//如果这张牌拥有三张 
        if(x.card[i]>=4)//这张牌拥有四张或五张,一样的都归为 四条  这个级别。 
        {
            four=i;
            if(x.card[i]==5)single_value+=i;//五张一样的 剩下那张归为单张 
        }
    }
/*
注意!如果一个大等级中,有多个排序的优先级
那么:
第一优先级 乘 0.01
第二优先级    0.0001
第三优先级    0.000001
因为最大的单张牌面“K”的值为13,超过10了,如果少乘一个0会影响到大等级。 
*/ 
    if(x.card[10]*x.card[11]*x.card[12]*x.card[13]*x.card[1]==1)x.level+=8.0;//皇家同花顺 ,永远滴神! 
    else if(line)//顺子  第7级 
    {
        x.level=7.0;
        x.level+=maxn*0.01;
    }
    else if(four)//四条 第6级 
    {
        x.level=6.0;
        x.level+=four*0.01+single_value*0.0001;
    }
    else if(three)//满堂红或三条 
    {
        if(couple1){//有一个对子,满堂红  第5级 
            x.level=5.0;
            x.level+=three*0.01+couple1*0.0001; 
        }
        else{//三条 第4级 
            x.level=4.0; 
            x.level+=three*0.01+single_value*0.0001;
        }
    }
    else if(couple1)//两对或对子 
    {
        if(couple2){//两对  第3级 
            x.level=3.0;
            x.level+=couple2*0.01+couple1*0.0001+single_value*0.000001;
        }
        else{//单个对子  第2级 
            x.level=2.0;
            x.level+=couple1*0.01+single_value*0.0001;
        }
    }
    else {//高牌  最弱的第1级 
        x.level=1.0;
        x.level+=single_value*0.0001;
    }
    f[cur]=x;//不用指针,将其转移给当前的这个f (因为f数组是全局变量) ,不然f数组的值不会改变的 
}

int main()
{
    int n,value;
    string tmp;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>f[i].name>>f[i].s;
        string str=f[i].s;
        for(int j=0,t=0;t<5;t++)//共五张牌 
        {
            if(str[j]!='1'){ //不为10 
                tmp=str[j];  //提取出这个字符 
                value=trans(tmp);//转换这个字符为对应的值 
                f[i].card[value]++;//该值的手牌数+1 
                j++;
            }
            else{   //遇到10了,只有 10 才会出现 1这个数字,因为牌值为1的是 A 
                f[i].card[10]++;
                j+=2;   //跳两格           
            }
        }
        deal_the_level_of(f[i],i);//处理f[i],记得带下标i 
    }
    sort(f,f+n,cmp);    //排序 
    for(int i=0;i<n;i++)cout<<f[i].name<<endl;  //输出 
    return 0;
}

Hard to TOT

Guess you like

Origin www.cnblogs.com/LiangYC1021/p/12357556.html