uva 11489 Integer Game

來源:互聯網
上載者:User

原題:
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的倍數的個數去判斷即可,不過,先手已經取過數了,答案要反著寫。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.