码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11489 Integer Game (博弈)

时间:2015-06-18 22:17:57      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

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

3
4
33
771

Sample Output

Case 1: S
Case 2: T
Case 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;
}


UVA 11489 Integer Game (博弈)

标签:

原文地址:http://blog.csdn.net/u013446688/article/details/46551375

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!