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\n",ans);
27     }
28     return 0;
29 }

时间: 2024-10-17 21:56:45

UVa11489的相关文章

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 - Integer Game(考思维,找规律)

从S开始时只能是两种情况: 1.现在总和已经是3的倍数了,那么因为每人每次只能拿走一个数,要保持拿走之后依然是3的倍数,那么就只能拿3,6,9这类数,用num统计一下,看看num奇偶性就知道谁最后拿了. 2.现在总和不是3的倍数,那么要么除3余1要么除3余2,就用num1和num2分别统计两种单个数的数目,看S第一下能不能拿完以后变成3的倍数,能就是按第一种情况分析了,不能T就赢了. #include<iostream> #include<cstdio> #include<c

UVA11489 - Integer Game(博弈)

题目链接 题意:有一连串的数字,两个人轮流取一个数,当谁取走数后,剩下的数的和不能被3整除,则这个人输了,求出先手是否能胜. 思路:当数只有一个时,先后必胜.当数大于两个时,先判断先手取完数后,剩下的数是否能被3整除,如果可以的话,接下去两个人轮流取的数都必须是3的整数倍.计算3的整数倍的个数就可以了. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm&

题解 UVa11489

题目大意 多组数据,每组数据给定一个整数字串,两个人每次从中抽数,要求每次剩余的数都是 \(3\) 的倍数,请求出谁会获胜. 分析 由于 \(p-1\) 的因数的倍数在 \(p\) 进制下的各位数字之和仍是其倍数,所以可以知道从第一次取完之后每次都要取 \(3\) 的倍数,统计即可. #include<bits/stdc++.h> using namespace std; int T, len; int a[3]; char s[1005]; int main() { scanf("

UVa 11489 整数游戏

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

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y