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, it’s 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 NN 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                             Output
for Sample Input


3

4

33

771


Case 1: S

Case 2: T

Case 3: T

题目大意:两个人博弈,轮流从一列数字里取数,要求剩下的数字之和必须是3的倍数,或者为空。不满足条件者输。

比较简单的博弈,两种情况,一种情况只有一个数字,后手输。第二种情况又分两种情况,第一种是先手可以取数,那么取完之后,接下来的人必取3的倍数,只要统计出三的倍数有多少个就ok了。

#include<stdio.h>
#include<string.h>
char a[1005];
int b[1005];
int main() {
    int t;
    scanf("%d%*c",&t);
    for(int ca=1;ca<=t;ca++) {
        scanf("%s",a);
        printf("Case %d: ",ca);
        int n=strlen(a);
        if(n == 1) {
            printf("S\n");
            continue;
        }
        int cnt=0 , sum=0 ,cnt2=0;
        for(int i=0;i<n;i++) {
            b[i]=a[i]-'0';
            sum+=b[i];
            if(a[i] == '3' || a[i] == '6' || a[i] == '9' ) {
                cnt++;
            } else {cnt2++;}
        }
        if(sum%3 == 0) {
            if(cnt%2 == 1) printf("S\n");
            else printf("T\n");
        } else {
            int i;
            for(i=0;i<n;i++) {
                if((sum-b[i])%3 == 0 ) {
                    break;

                }
            }
            if( i == n ) {printf("T\n");continue;}
            if(cnt%2 == 1) printf("T\n");
            else printf("S\n");
        }
    }
    return 0;
}



Uva 11489 - Integer Game

时间: 2024-10-09 05:13:57

Uva 11489 - Integer Game的相关文章

UVA 11489 Integer Game (博弈)

1227: Integer Game Time Limit: 1 Sec  Memory Limit:128 MB Submit: 9  Solved: 4 [Submit][Status][Web Board] Description 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

UVa 424 Integer Inquiry 【大数相加】

解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当输入a的第一个字符为0的时候结束运算,输出结果.  Integer Inquiry  One of the firstusers of BIT's new supercomputer was Chip Diller. He extended his explorationof powers of 3

UVa 11489 整数游戏

https://vjudge.net/problem/UVA-11489 题意: 给出一个数字串n,两个人轮流从中取出一个数字,要求每次取完之后剩下的数是3的倍数,不能取数者输. 思路: 要想取掉一个数后总和还是的倍数,那么取掉的数必须得是3的倍数. 分两种情况: ①数字串总和为3的倍数,此时只需要看数字串中3的倍数的个数,奇数个的话先手赢,否则后手赢. ②数字串总和不为3,那么它会余1or余2,如果余1,那么查看数字串中是否存在%3后余1的数,如果有,则转化成了①情况,否则先手就输.(同理分析

uva 424 Integer Inquiry

#include<iostream> #include<cctype> #include<cstdlib> #include<cstring> using namespace std; int main(){ char a[110][110]; int result[110]; int num = 0,index; memset(a,'a',sizeof(a)); //没有初始化,wa 了好几次 memset(result,0,sizeof(result))

Integer Game(UVA11489)3的倍数

K - Integer Game Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 11489 题意:在n中取数字,使剩下的数是3的倍数,不能取则失败. 思路:如果能使当前数为3的倍数,数字和必是3的倍数.要保持这种状态,先对每一位对3取余,统计cnt[0],cnt[1],cnt[2]; 的个数,整个串能否被3整除取决于ans=(cnt[1]+cnt[

UVa11489

11489 Integer GameTwo 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 theinteger and passes the resulting number to the other

Uva 1069 Always an Integer ( 数学 )

Uva 1069 Always an Integer ( 数学 ) #include <cstdio> #include <cstring> #include <algorithm> #include <cctype> using namespace std; typedef long long LL; #define MAXN 105 #define CLR( a, b ) memset( a, b, sizeof(a) ) LL c[ MAXN ], d

uva 1069 - Always an integer(数学+暴力)

题目链接:uva 1069 - Always an integer 题目大意:给出一个多次多项式,问说是否对于任意正整数n来说结构均为整数. 解题思路:首先处理出字符串,然后枚举n从1到k+1判断即可,k为多项式中出现过的最大幂数指. P为多项式,d为除数,k为多项式中最大的次数 当k=0时,P中不存在n变量,所以直接计算判断即可 当k=1时,P是一次多项式,那么P(n+1)?P(n)=a,a为常数,也就是说P(i)为等差数列,如果首项P(1)和公差P(2)-P(1)为d的倍数即可,等价与判断P

Integer Inquiry UVA 424

#include <stdio.h> #include <string.h> #define MAXN 100+5 #define MAXL 100+5 char addend[MAXN][MAXL];//保存加数 char ans[MAXL];//保存结果 int len[MAXN];//保存每个加数的长度 int main(){ int cnt=0;//加数的个数 int max=0;//最长的加数的长度 int i,k,j=MAXL-1,sum,carry; //freope