UVA 11489 I - Integer Game

Qustion:
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
题意大意:输入的一个非零数字字符串,两个人一次分别取一个数,要求每次取出数后,剩下的数的总和为3的倍数,直至一方不能再取出数为止。
(http://acm.hust.edu.cn/vjudge/contest/121559#problem/I)
思路:除了第一次外,以后每次取出的数字是3的倍数(在第一次能成功取出的条件下)。如果字符串关于3的倍数是偶数时,则起始方获胜,否则乙方获胜。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
    int n;
    char s[1005];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s);
        printf("Case %d: ",i);
        int sum=0,res=0;
        for(int j=0;j<strlen(s);j++)
        {
            int t=(int)s[j]-48;
            sum+=t;
            if(t%3==0)
                res++;
        }     //计算出关于3的倍数数字的个数
        int temp=sum%3,flag=0;
        if(temp==0)
        {
            res-=1;
            temp+=3;
        }     //判断和是否为3的倍数,如果是的话第一次就应取出3的倍数,在3的倍数个数上减去一个
        for(int j=0;j<strlen(s);j++)
        {
            if(s[j]==char(temp+48)||s[j]==char(temp+48+3)||s[j]==char(temp+48+6))
            {
                flag=1;
                break;
            }
        }
        //判断是否能取出第一个数
        if(flag==0)
            printf("T\n");
        else
        {
            if(temp==0)
            res-=1;
            if(res%2==0)
               printf("S\n");
            else printf("T\n");
        }
    }
    return 0;
}

体会:做题时考虑特殊情况,例如本题的第一次情况特殊,其余的都是3的倍数。*重点内容*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值