-
Notifications
You must be signed in to change notification settings - Fork 0
/
score_hand.c
60 lines (46 loc) · 1.03 KB
/
score_hand.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<stdio.h>
int score_hand(size_t n, const char cards[n]) {
// <---- hajime!
int a_cnt =0,sum=0;
for(int x =0; x < n; x++)
{
switch(cards[x])
{
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
sum +=cards[x]-'0';
break;
case 'A':
a_cnt++;
break;
case 'J':
case 'Q':
case 'K':
case 'T':
sum+=10;
break;
default:
break;
}
}
if(a_cnt)
{
if(a_cnt ==1)
return (sum + 11) >21 ? (sum+ a_cnt):(sum+11 );
if(a_cnt > 1)
return (sum + 11) >=21 ? (sum+ a_cnt):(sum+11 + a_cnt -1 );
}
return sum;
}
int main()
{
char cards[]= {'8', 'A', 'A'};
printf("%lu\r\n",score_hand(sizeof(cards),cards));
return 0;
}