Card Value


General Statement: Read a collection of playing cards and compute the total numerical value of that “hand”.

Input: The first line of the data set for this problem is an integer that represents the number of strings that follow. Each string is on a separate line.
The card value is first and the suit is second. There is an underscore (_) between “cards”.

Output: Output the calculated sum followed by the word POINTS.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: All letters are upper case.
The maximum number of cards in a hand is 10.
Each card occurs only 1 time in a hand.

Discussion:
The suits are: C = Clubs
D = Diamonds
H = Hearts
S = Spades

Card values are: 2..10 = face value
A = 11
J, Q, K = 10

If only 2 suits are in the hand, add 10 points.
If all cards are of the same single suit, add 25 points.

Sample Input:
3
2C_10H_AD_KH_4S
9H_QD_QC_3H_3D_AC_AH
7C_8C_AC_4D

Sample Output:
37 POINTS
57 POINTS
40 POINTS

Solutions :

#include <stdio.h>
#include <string.h>

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

    while ( dataset-- ) {
        char input [50];
        scanf ("%s", input);

        int length = strlen (input);
        int c = 0;
        int d = 0;
        int h = 0;
        int s = 0;
        int card_value = 0;
        int i = 0;

        while ( i < length ) {
            switch (input [i]) {
                case '2' :
                case '3' :
                case '4' :
                case '5' :
                case '6' :
                case '7' :
                case '8' :
                case '9' :
                card_value += input [i] - '0';
                break;

                case '1' :
                card_value += 10;
                i++; // '0' in 10
                break;

                case 'A' :
                card_value += 11;
                break;

                case 'J' :
                case 'Q' :
                case 'K' :
                card_value += 10;

                case 'C' :
                c = 1;
                i++; // underscore
                break;

                case 'D' :
                d = 1;
                i++; // underscore
                break;

                case 'H' :
                h = 1;
                i++; // underscore
                break;

                case 'S' :
                s = 1;
                i++; // underscore
                break;
            }

            i++;
        }

        if ( c + d + h + s == 2 )
            card_value += 10;

        else if ( c + d + h + s == 1 )
            card_value += 25;

        printf ("%d POINTS\n", card_value);
    }


    return 0;
}
Advertisements

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