Two players, S and T, are playing a game where they make alternate moves. S plays
first.
In this game, they start with an integer N. In each move, a player removes one digit from the integer and passes the resulting number to the other player. The game continues in this fashion until a player finds he/she has no digit
to remove when that player is declared as the loser.
With this restriction, it’s obvious that if the number of digits in N is odd then S wins
otherwise T wins. To make the game more interesting, we apply one additional constraint. A player can remove a particular digit if the sum of digits of the resulting number is a multiple of 3 or there are no digits
left.
Suppose N = 1234. S has 4 possible moves. That is, he can remove 1, 2, 3, or 4. Of
these, two of them are valid moves.
- Removal of 4 results in 123 and the sum of digits = 1 + 2 + 3 = 6; 6 is a multiple of 3.
- Removal of 1 results in 234 and the sum of digits = 2 + 3 + 4 = 9; 9 is a multiple of 3.
The other two moves are invalid.
If both players play perfectly, who wins?
Input
The first line of input is an integer T(T<60) that determines the number of test cases.
Each case is a line that contains a positive integer N. N has at most 1000 digits and does not contain any zeros.
Output
For each case, output the case number starting from 1. If S wins
then output ‘S’ otherwise output ‘T’.
Sample Input Output
for Sample Input
3 4 33 771 |
Case 1: S Case 2: T Case 3: T |
题目大意:两个人博弈,轮流从一列数字里取数,要求剩下的数字之和必须是3的倍数,或者为空。不满足条件者输。
比较简单的博弈,两种情况,一种情况只有一个数字,后手输。第二种情况又分两种情况,第一种是先手可以取数,那么取完之后,接下来的人必取3的倍数,只要统计出三的倍数有多少个就ok了。
#include<stdio.h> #include<string.h> char a[1005]; int b[1005]; int main() { int t; scanf("%d%*c",&t); for(int ca=1;ca<=t;ca++) { scanf("%s",a); printf("Case %d: ",ca); int n=strlen(a); if(n == 1) { printf("S\n"); continue; } int cnt=0 , sum=0 ,cnt2=0; for(int i=0;i<n;i++) { b[i]=a[i]-'0'; sum+=b[i]; if(a[i] == '3' || a[i] == '6' || a[i] == '9' ) { cnt++; } else {cnt2++;} } if(sum%3 == 0) { if(cnt%2 == 1) printf("S\n"); else printf("T\n"); } else { int i; for(i=0;i<n;i++) { if((sum-b[i])%3 == 0 ) { break; } } if( i == n ) {printf("T\n");continue;} if(cnt%2 == 1) printf("T\n"); else printf("S\n"); } } return 0; }
Uva 11489 - Integer Game