UVA 11489 Integer Game (博弈)

来源:互联网 发布:qq三国桃园第一js蘑菇 编辑:程序博客网 时间:2024/05/18 15:50

1227: Integer Game

Time Limit: 1 Sec  Memory Limit:128 MB

Submit: 9  Solved: 4
[Submit][Status][Web Board]

Description


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

3433771

Sample Output

Case 1: SCase 2: TCase 3: T



解析:

1.总和不为3的倍数时,判断第一个人能否取。
        (1)能取,判断有多少个数为三的倍数即可。
        (2)不能取, 输出T。
2.总和为3的倍数时,直接判断判断有多少个数为三的倍数即可。




AC代码;

#include <bits/stdc++.h>using namespace std;char s[1002];bool vis[4];int main(){    #ifdef sxk        freopen("in.txt", "r", stdin);    #endif // sxk    int T;    scanf("%d", &T);    for(int t=1; t<=T; t++){        memset(vis, false, sizeof(vis));        printf("Case %d: ", t);        scanf("%s", s);        int len = strlen(s);        int cnt = 0, sum = 0;        for(int i=0; i<len; i++){            int foo = s[i] - '0';            sum += foo;            if(foo % 3 == 0) cnt ++;            vis[foo % 3] = true;        }        if(sum % 3){            if(vis[sum % 3]) puts(cnt % 2 ? "T" : "S");            else puts("T");        }        else puts(cnt % 2 ? "S" : "T");    }    return 0;}


0 0