CodeChef (POKER)


// http://www.codechef.com/problems/POKER/

#include <stdio.h>

int main ()
{
    int dataset;
    scanf ("%d", &dataset);
    getchar ();

    while ( dataset-- ) {
        char input [20];

        gets (input);

        int rank [14] = {0};
        int suit [4] = {0};

        for ( int i = 0; i < 13; i += 3 ) {
            if ( input [i] >= 50 && input [i] <= 57 )
                rank [input [i] - 48]++;
            else if ( input [i] == 'A' )
                rank [1]++;
            else if ( input [i] == 'T' )
                rank [10]++;
            else if ( input [i] == 'J' )
                rank [11]++;
            else if ( input [i] == 'Q' )
                rank [12]++;
            else if ( input [i] == 'K' )
                rank [13]++;
        }

        for ( int i = 1; i < 14 ; i += 3 ) {
            if ( input [i] == 'S' )
                suit [0]++;
            else if ( input [i] == 'H' )
                suit [1]++;
            else if ( input [i] == 'D' )
                suit [2]++;
            else if ( input [i] == 'C' )
                suit [3]++;
        }

        bool flag = false;

        switch (true) {
            case 1 :
                if ( (suit [0] == 5 || suit [1] == 5 || suit [2] == 5 || suit [3] == 5)
                && (rank [1] && rank [10] && rank [11] && rank [12] && rank [13]) ) {
                    printf ("royal flush\n");
                    break;
                }

            case 2 :
                if ( suit [0] == 5 || suit [1] == 5 || suit [2] == 5 || suit [3] == 5 ) {

                    for ( int i = 1; i <= 9; i++ ) {
                        if ( rank [i] && rank [i + 1] && rank [i + 2] && rank [i + 3] && rank [i + 4]) {
                            printf ("straight flush\n");
                            flag = true;
                        }
                    }

                    if ( flag )
                        break;
                }

            case 3 :
                for ( int i = 1; i <= 13; i++ ) {
                    if ( rank [i] == 4 ) {
                        printf ("four of a kind\n");
                        flag = true;
                    }
                }

                if ( flag )
                    break;

            case 4 :
                for ( int i = 1; i <= 13; i++ ) {
                    for ( int j = 1; j <= 13; j++ ) {
                        if ( rank [i] + rank [j] == 5 && (rank [i] - rank [j] == 1 || rank [i] - rank [j] == -1) ) {
                            printf ("full house\n");
                            flag = true;
                            i = j = 13;
                        }
                    }
                }

                if ( flag )
                    break;

            case 5 :
                if ( suit [0] == 5 || suit [1] == 5 || suit [2] == 5 || suit [3] == 5 ) {
                    printf ("flush\n");
                    break;
                }

            case 6 :
                for ( int i = 1; i <= 9; i++ ) {
                    if ( rank [i] && rank [i + 1] && rank [i + 2] && rank [i + 3] && rank [i + 4]) {
                        printf ("straight\n");
                        flag = true;
                    }
                }

                if ( flag )
                    break;

            case 7 :
                for ( int i = 1; i <= 13; i++ ) {
                    if ( rank [i] == 3 ) {
                        printf ("three of a kind\n");
                        flag = true;
                    }
                }

                if ( flag )
                    break;

            case 8 :
                for ( int i = 1; i <= 13; i++ ) {
                    for ( int j = 1; j <= 13; j++ ) {
                        if ( i != j && rank [i] + rank [j] == 4 && (rank [i] - rank [j] == 0) ) {
                            printf ("two pairs\n");
                            flag = true;
                            i = j = 13;
                        }
                    }
                }

                if ( flag )
                    break;

            case 9 :
                for ( int i = 1; i <= 13; i++ ) {
                    if ( rank [i] == 2 ) {
                        printf ("pair\n");
                        flag = true;
                    }
                }

                if ( flag )
                    break;

            default :
                printf ("high card\n");
        }

    }

    return 0;
}

4 thoughts on “CodeChef (POKER)

  1. sir i am having problem with submitting the code
    the code works fine on my laptop but on Code chef it says wrong output

    Well in that case i need a tricky case!!

    i have my code:

    #include<iostream>
    #define A 14
    #define K 13
    #define Q 12
    #define J 11
    
    using namespace std;
    
    int main(){
        int t;
        scanf("%d",&t);;
        for(int p=0;p<t;p++){
        int l=0;
        bool flag=0;
        int S[15]={0};
        int H[15]={0};
        int D[15]={0};
        int C[15]={0};
        int spade_n=0;
        int heart_n=0;
        int diamond_n=0;
        int club_n=0;
        while(l<5){
                   l++;
                char suit;
                char number_c;
                int number;
                cin>>number_c;
    //            cout<<number_c;
                if(number_c=='A'){number=14;}
                else if(number_c=='K'){number=13;}
                else if(number_c=='Q'){number=12;}
                else if(number_c=='J'){number=11;}
                else if(number_c=='T'){number=10;}
                else{number=(int)(number_c-48);}
        //        cout<<number<<endl;
      //          cout<<"now suit"<<endl;
                cin>>suit;
                if(suit=='S'){
                              spade_n++;
                              S[number]++;
                              if(number==14){S[1]++;}
                }
                if(suit=='H'){
                              heart_n++;
                              H[number]++;
                              if(number==14){H[1]++;}}
                if(suit=='D'){
                              diamond_n++;
                              D[number]++;
                              if(number==14){D[1]++;}
                }
                if(suit=='C'){
                              club_n++;
                              C[number]++;
                              if(number==14){C[1]++;}
                }
        }
       // cout<<"No of cars of each are : spade "<<spade_n<<"\nheart:"<<heart_n<<"\ndiamond "<<diamond_n<<"\nclub "<<club_n<<endl;
        
                if(S[14]==1 && S[13]==1 && S[12]==1 && S[11]==1 && S[10]==1){cout<<"royal flush"<<endl;flag==1;goto out;}
                if(H[14]==1 && H[13]==1 && H[12]==1 && H[11]==1 && H[10]==1){cout<<"royal flush"<<endl;flag==1;goto out;}
                if(D[14]==1 && D[13]==1 && D[12]==1 && D[11]==1 && D[10]==1){cout<<"royal flush"<<endl;flag==1;goto out;}
                if(C[14]==1 && C[13]==1 && C[12]==1 && C[11]==1 && C[10]==1){cout<<"royal flush"<<endl;flag ==1;goto out;}
                
                if(flag==0){
                            if(spade_n==5){
                            int cntr=0;
                             for(int i=0;i<15;i++){
                                     if(S[i]==0){cntr=0;}
                                     else{cntr++;}
                                     if(cntr==5){flag==1;cout<<"straight flush"<<endl;goto out;}
                                     }
                                     }
                             else if(heart_n==5){
                                  int cntr=0;
                                  for(int i=0;i<15;i++){
                                          if(H[i]==0){cntr=0;}
                                          else{cntr++;}
                                          if(cntr==5){flag=1;cout<<"straight flush"<<endl;goto out;}
                                  }
                             }
                             else if(diamond_n==5){
                             int cntr=0;
                             for(int i=0;i<15;i++){
                                     if(D[i]==0){cntr=0;}
                                     else{cntr++;}
                                     if(cntr==5){flag=1;cout<<"straight flush"<<endl;goto out;}
                                     }
                             }
                             else if(club_n==5){
                             int cntr=0;
                                 for(int i=0;i<15;i++){
                                     if(C[i]==0){cntr=0;}
                                     else{cntr++;}
                                     if(cntr==5){flag=1;cout<<"straight flush"<<endl;goto out;}
                                     }
                             }
                }
                if(flag==0){
                            if(spade_n<=2 && heart_n<=2 && diamond_n<=2 && club_n<=2){
                            for(int i=0;i<15;i++){
                                    if(S[i]==1 && H[i]==1 && D[i]==1 && C[i]==1){cout<<"four of a kind"<<endl;flag==1;goto out;}
                            }
                            for(int i=0;i<15;i++){
                                    if(S[i]==1 && H[i]==1 && D[i]==1 || S[i]==1 && D[i]==1 && C[i]==1 || S[i]==1 && H[i]==1 && C[i]==1 ||H[i]==1 && D[i]==1 && C[i]==1){
                                        for(int j=0;j<15;j++ && i!=j){
                                                if(S[j]==1 && H[j]==1|| S[j]==1 && D[j]==1|| S[j]==1 && C[j]==1 ||H[j]==1 && D[j]==1 ||H[j]==1 && C[j]==1|| D[j]==1 && C[j]==1)
                                                {flag ==1;cout<<"full house"<<endl;goto out;}
                                                }
                                        }
                                    }
                                    
                             }
                }
                if(flag==0){              
                                          if(spade_n==5){cout<<"flush"<<endl;goto out;}
                                          else if(club_n==5){cout<<"flush"<<endl;goto out;}
                                          else if(heart_n==5){cout<<"flush"<<endl;goto out;}
                                          else if(diamond_n==5){cout<<"flush"<<endl;goto out;}
                }
                
                if(flag==0){
                            for(int i=0;i<11;i++){
                                    if(S[i]==1||H[i]==1||D[i]==1||C[i]==1){
                                    if(S[i+1]==1||H[i+1]==1||D[i+1]==1||C[i+1]==1){
                                    if(S[i+2]==1||H[i+2]==1||D[i+2]==1||C[i+2]==1){
                                    if(S[i+3]==1||H[i+3]==1||D[i+3]==1||C[i+3]==1){
                                    if(S[i+4]==1||H[i+4]==1||D[i+4]==1||C[i+4]==1){cout<<"straight"<<endl;flag=1;goto out;}
                                    }}}}
                            }
                }
                if(flag==0){
                            for(int i=0;i<15;i++){
                                    if(S[i]==1 && H[i]==1 && D[i]==1 || S[i]==1 && D[i]==1 && C[i]==1 || S[i]==1 && H[i]==1 && C[i]==1 ||H[i]==1 && D[i]==1 && C[i]==1){
                                               cout<<"three of a kind"<<endl;flag==1;goto out;
                                    }
                            }
                }
                            
                if(flag==0){
                            for(int j=0;j<15;j++){
                                    if(S[j]==1 && H[j]==1|| S[j]==1 && D[j]==1|| S[j]==1&& C[j]==1 ||H[j]==1 && D[j]==1 ||H[j]==1 && C[j]==1|| D[j]==1 && C[j]==1)
                                    {
                                               for(int i=0;i<15;i++ && i!=j){
                                                       if(S[i]==1 && H[i]==1|| S[i]==1 && D[i]==1|| S[i]==1&& C[i]==1 ||H[i]==1 && D[i]==1 ||H[i]==1 && C[i]==1|| D[i]==1 && C[i]==1){
                                                       flag ==1;cout<<"two pairs"<<endl;goto out;
                                                       }
                                               }
                                    }
                            }           
                }
                
                if(flag==0){
                            for(int j=0;j<15;j++){
                                    if(S[j]==1 && H[j]==1|| S[j]==1 && D[j]==1|| S[j]==1&& C[j]==1 ||H[j]==1 && D[j]==1 ||H[j]==1 && C[j]==1|| D[j]==1 && C[j]==1)
                                    {flag ==1;cout<<"pair"<<endl;goto out;}
                            }
                }
                if(flag==0){cout<<"high card"<<endl;flag=1;goto out;}
                out:;    
        }
        system("pause");
        return 0;
    }
    

    hoping u may help

  2. @sukhmeet
    hello Sukhmeet, it would be better if u can mail me the code, tausiq19[at]gmail[dot]com
    i’ll try to figure out the faulty portion .. thanks

  3. @sukhmeet
    u can try these input :

    Input : 
    5
    AH AC KH QC JD
    AH AS AD KS QC
    TS TC 6C 4H 2H
    2D 2H 8S 5C 4C
    8C 8H 8D AC 2D
    
    Correct output :
    pair
    three of a kind
    pair
    pair
    three of a kind
    
    ur code gives :
    two pairs
    full house
    two pairs
    two pairs
    full house
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s