uva 11489 Integer Game

原题:
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
中文:
给你一个1000位数,现在两个人轮流从这些数中取走一个数字,要求拿走后剩下的数字可以被3整除。不能取数的人判定输。先手胜输出S后手胜输出T。(注意,如果给的初始数据不能被3整除,不能直接判定后手胜!)

#include <bits/stdc++.h>
using namespace std;
//fstream in,out;
int main()
{
    ios::sync_with_stdio(false);
    string s;
    int mark3,tot,mark2,mark1;
    int t,k=1;
//  in.open("data.txt");
//  out.open("answer.txt");
    cin>>t;
    while(t--)
    {
        cin>>s;
        tot=mark3=mark2=mark1=0;
        for(int i=0;i<s.size();i++)
        {
            tot+=s[i]-'0';
            if(s[i]=='0'||s[i]=='3'||s[i]=='6'||s[i]=='9')
                mark3++;
            if(s[i]=='1'||s[i]=='4'||s[i]=='7')
                mark1++;
            if(s[i]=='2'||s[i]=='5'||s[i]=='8')
                mark2++;
        }
        cout<<"Case "<<k++<<": ";
        if(s.size()==1)
        {
            cout<<'S'<<endl;
            continue;
        }
        if(tot%3==0)
        {
            if(mark3%2)
                cout<<'S'<<endl;
            else
                cout<<'T'<<endl;
        }
        else
        {
            if(tot%3==1)
            {
                if(mark1==0)
                    cout<<'T'<<endl;
                else
                {
                    if(mark3%2)
                        cout<<'T'<<endl;
                    else
                        cout<<'S'<<endl;
                }
            }
            else
            {
                if(mark2==0)
                    cout<<'T'<<endl;
                else
                {
                    if(mark3%2)
                        cout<<'T'<<endl;
                    else
                        cout<<'S'<<endl;
                }
            }
        }
    }
//  in.close();
//  out.close();
    return 0;
}

解答:
简单的博弈问题
首先要知道一个小知识点就是如果一个数的各位的和能被3整除,那么这个数就能被3整除。
两个人轮流取数,首先判断这个数能不能被3整除,如果能。那么判断这个数里面有多少个3的倍数,因为在这个数是3的倍数的前提下,只有每次取出3的倍数,剩下的数的和才能是3的倍数。
如果这个数不是3的倍数,那么就要先取出一个和这个数同余的数,比如890899这个数的和是43,模上3为1,那么就要取出1,4,7这三个数之一,如果没有就是先手负,如果有这个数,取出一个此数,那么剩下的数的和就一定能被3正常,继续用3的倍数的个数去判断即可,不过,先手已经取过数了,答案要反着写。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值