C-Integer Game
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, its 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
3
4
33
771
Sample Output
Case 1: S
Case 2: T
Case 3: T
给你一个长度不超过1000,且各位不为0的数,S,T两个人每人依次去除一个数字,如果剩下的数字之和为3的倍数则继续,直到不能去除为止,输出胜利者S或T。
用cnt记录3的倍数数字个数,先手去除一个数字使sum合规,失败则输出T。如果成功,检查被去除的数字是否为3的倍数,是则cnt减一。
然后如果cnt为偶数,输出S,奇数输出T。
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
int T;
cin >> T;
for (int kase = 1; kase <= T; kase++)
{
char N[1005];
cin >> N;
printf("Case %d: ", kase);
int sum = 0,len=strlen(N),cnt=0;
for (int i = 0; i < len; i++) {
sum += (N[i] - '0');
if ((N[i] - '0') % 3 == 0) cnt++;
}
int i = 0;
for (; i < len; i++) {
if ((sum - (N[i] - '0')) % 3 == 0) {
if ((N[i] - '0') % 3 == 0) cnt--;
break;
}
}
if (i == len) printf("T\n");
else {
if (cnt % 2 == 0) printf("S\n");
else printf("T\n");
}
}
return 0;
}