UVa 1489 - Integer Game

原创 2015年07月09日 12:42:58

1489 - Integer Game

Two players, S and T, are playing a game where they make alternate moves. S plays rst.
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 nds 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 rst 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


水题,找到规律即可。



#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int n;
    cin>>n;
    for(int k=1; k<=n; k++)
    {
        char a[1005];
        cin>>a;
        int sum=0,num=0;
        for(int i=0; a[i]; i++)
        {
            a[i]=a[i]-'0';
            if(a[i]%3==0)
                sum++;
            num+=a[i];
        }
        if(num%3==0)
        {
            if(sum%2==0)
                printf("Case %d: T\n",k);
            else
                printf("Case %d: S\n",k);
            continue;
        }
        int flag=1;
        if(num%3==1)
        {
            for(int i=0; a[i]; i++)
            {
                if(a[i]%3==1)
                {
                    flag=0;
                    if(sum%2==0)
                        printf("Case %d: S\n",k);
                    else
                        printf("Case %d: T\n",k);
                    break;
                }
            }
            if(flag==1)
                printf("Case %d: T\n",k);
            continue;
        }
        if(num%3==2)
        {
            for(int i=0; a[i]; i++)
            {
                if(a[i]%3==2)
                {
                    flag=0;
                    if(sum%2==0)
                        printf("Case %d: S\n",k);
                    else
                        printf("Case %d: T\n",k);
                    break;
                }
            }
            if(flag==1)
                printf("Case %d: T\n",k);
            continue;
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

BNU19907 UVA11489 Integer Game

Integer Game Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Orig...

Integer Game(UVA11489)3的倍数

K - Integer Game Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S...

UVa 11489 - Integer Game (简单博弈 脑筋急转弯)

I Integer Game     Two players, S and T, are playing a game where they make alte...

UVa 11489 Integer Game (博弈&想法题)

11489 - Integer Game Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php?option=com_onl...

Uva 11489 - Integer Game 解题报告(简单推理)

Integer Game Two players, S and T, are playing a game where they make alternate moves. S plays fir...
  • kbdwo
  • kbdwo
  • 2014年03月24日 21:12
  • 494

UVa 424 - Integer Inquiry

这道题目就是大整数的相加,类似于给你一个很大的数求阶乘,起主导思想是一样的,说不清楚,看代码吧: #include #include int main() { int i,j,n,m,t...

uva1069 Always an integer【解法一】

随机化
  • sdfzyhx
  • sdfzyhx
  • 2016年12月17日 16:59
  • 134

UVa 424 Integer Inquiry(整数查询)

424 Integer Inquiry(整数查询)(Big Number) 经典的大整数相加! 通过这个题目,可以找到许多种解决一个题目的办法。而且,在这题中我们可以发现算法竞赛判定的一些陷阱,说白...

UVA 1228 Integer Transmission(dp)

题意:将一个n位的整数从左到右
  • qian99
  • qian99
  • 2014年09月12日 11:55
  • 749

UVa 424 Integer Inquiry

之前杭电上也做过a + b的高精度的题
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVa 1489 - Integer Game
举报原因:
原因补充:

(最多只允许输入30个字)