# UVa 1489 - Integer Game

225人阅读 评论(0)

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;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：36319次
• 积分：1313
• 等级：
• 排名：千里之外
• 原创：100篇
• 转载：15篇
• 译文：0篇
• 评论：7条
文章分类
评论排行
最新评论