zoukankan      html  css  js  c++  java
  • UVa11489

    11489 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

    题意:

           给出一字符串N,两人轮流从中取出一数字,要求每次取完之后剩下的数是3的倍数,不能取者输。N由不超过1000个非零数字组成。如果先手胜则输出S否则输出N。

    分析:

           要想取走一个数字之后剩下的数能被3整除,我们只需要统计N的每一个数字,其中能被3整除的数字有cnt个,数字之和为sum。那么我们只需要遍历每一个数字模拟先手取数字的情形,如果取走某个数字之后剩余的sum被3整除,则后手只能取走是3的倍数的数字,否则后手将会失败。如果先手无论取走哪一个数字都无法使得剩余的sum值被3整除则先手失败。

     1 #include <cstdio>
     2 #include <cstring>
     3 #define MAX_LEN 1000
     4 char str[MAX_LEN + 1];
     5 int main(){
     6     int T; scanf("%d",&T);
     7     for(int kase = 1 ; kase <= T ; kase++){
     8         scanf("%s",str);
     9         char ans = 'T';
    10         printf("Case %d: ",kase);
    11         int cnt = 0,sum = 0,len = strlen(str);
    12         for(int i = 0 ; i < len ; i++){
    13             sum += str[i] - '0';
    14             if((str[i] - '0') % 3 == 0) cnt++;
    15         }
    16         for(int i = 0 ; i < len ; i++){
    17             int tmp = sum - (str[i] - '0'),tmpcnt = cnt;
    18             if(tmp % 3 == 0){
    19                 if((str[i] - '0') % 3 == 0) tmpcnt = cnt - 1;
    20                 if(!(tmpcnt & 1)){
    21                     ans = 'S';
    22                     break;
    23                 }
    24             }
    25         }
    26         printf("%c
    ",ans);
    27     }
    28     return 0;
    29 }
    View Code
  • 相关阅读:
    csp-s模拟 77/78 (达哥专场)
    csp-s 模拟76
    csp-s模拟75 导弹袭击
    反思集
    模拟69/70 考试反思
    抱大腿
    csp-s模拟61 甜圈
    实时记录
    好题思路集汇
    半集训反思 8.28 「路,还是要自己走的」
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5887275.html
Copyright © 2011-2022 走看看