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
  • 相关阅读:
    css js 解除网页无法选择进而复制的限制,bd文库无法复制
    Git命令简记
    DDD基本概念-未完成
    多线程隙-IO模型(BIO、NIO、AIO)
    RabbitMQ笔记-保证消息队列高可用
    关于fiddler手机抓包
    spring控制反转是谁在何时何地反转给谁?依赖注入??
    Cookie、Session、Token的区别
    详解Redis中两种持久化机制RDB和AOF
    Java中线程池的抛出策略、阻塞队列、内存溢出
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5887275.html
Copyright © 2011-2022 走看看