Você está na página 1de 4

10/23/2017 Card Value | Solved Programming Problems

Card Value

OCTOBER 11, 2009 / SHAHAB

i
Rate This

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 forma ed exactly like that for the sample output
given below.

Assumptions: All le ers are upper case.


The maximum number of cards in a hand is 10.
Each card occurs only 1 time in a hand.

Discussion:
https://tausiq.wordpress.com/2009/10/11/card-value/ 1/4
10/23/2017 Card Value | Solved Programming Problems

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 :

1 #include <stdio.h>
2 #include <string.h>
3
4 int main ()
5 {
6 int dataset;
7 scanf ("%d", &dataset);
8
9 while ( dataset-- ) {
10 char input [50];
11 scanf ("%s", input);
12
13 int length = strlen (input);
14 int c = 0;
15 int d = 0;
16 int h = 0;
17 int s = 0;
18 int card_value = 0;
19 int i = 0;
20
21 while ( i < length ) {
22 switch (input [i]) {
23 case '2' :
24 case '3' :
25 case '4' :
26 case '5' :
27 case '6' :
28 case '7' :
29 case '8'
https://tausiq.wordpress.com/2009/10/11/card-value/ : 2/4
10/23/2017 Card Value | Solved Programming Problems

29 case '8' :
30 case '9' :
31 card_value += input [i] - '0';
32 break;
33
34 case '1' :
35 card_value += 10;
36 i++; // '0' in 10
37 break;
38
39 case 'A' :
40 card_value += 11;
41 break;
42
43 case 'J' :
44 case 'Q' :
45 case 'K' :
46 card_value += 10;
47
48 case 'C' :
49 c = 1;
50 i++; // underscore
51 break;
52
53 case 'D' :
54 d = 1;
55 i++; // underscore
56 break;
57
58 case 'H' :
59 h = 1;
60 i++; // underscore
61 break;
62
63 case 'S' :
64 s = 1;
65 i++; // underscore
66 break;
67 }
68
69 i++;
70 }
71
72 if ( c + d + h + s == 2 )
73 card_value += 10;
74
75 else if ( c + d + h + s == 1 )
76 card_value += 25;
77
78 printf ("%d POINTS\n", card_value);
79 }
80
81
82 return 0;
83 }

https://tausiq.wordpress.com/2009/10/11/card-value/ 3/4
10/23/2017 Card Value | Solved Programming Problems
Advertisements

Easy Problems

https://tausiq.wordpress.com/2009/10/11/card-value/ 4/4

Você também pode gostar