UVA 11489 Integer Game (博弈)
2015-06-18 19:23
435 查看
1227: Integer Game
Time Limit: 1 Sec Memory Limit:128 MB
Submit: 9 Solved: 4
[Submit][Status][Web
Board]
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?
解析:
1.总和不为3的倍数时,判断第一个人能否取。
(1)能取,判断有多少个数为三的倍数即可。
(2)不能取, 输出T。
2.总和为3的倍数时,直接判断判断有多少个数为三的倍数即可。
AC代码;
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. 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
解析:
1.总和不为3的倍数时,判断第一个人能否取。
(1)能取,判断有多少个数为三的倍数即可。
(2)不能取, 输出T。
2.总和为3的倍数时,直接判断判断有多少个数为三的倍数即可。
AC代码;
#include <bits/stdc++.h> using namespace std; char s[1002]; bool vis[4]; int main(){ #ifdef sxk freopen("in.txt", "r", stdin); #endif // sxk int T; scanf("%d", &T); for(int t=1; t<=T; t++){ memset(vis, false, sizeof(vis)); printf("Case %d: ", t); scanf("%s", s); int len = strlen(s); int cnt = 0, sum = 0; for(int i=0; i<len; i++){ int foo = s[i] - '0'; sum += foo; if(foo % 3 == 0) cnt ++; vis[foo % 3] = true; } if(sum % 3){ if(vis[sum % 3]) puts(cnt % 2 ? "T" : "S"); else puts("T"); } else puts(cnt % 2 ? "S" : "T"); } return 0; }
相关文章推荐
- 阅读《构建之法》第10,11,12章
- 学习汇编的重要意义!!
- C/C++ 马踏棋盘实现
- .Net "command line arguments will not be passed" message
- Min Stack -- leetcode
- Apriori(先验)的java实现
- 《一个程序猿的生命周期》读后感
- JavaScript code to let users double-click to move back to the top of the page
- 思科OSPF配置实例
- hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题
- 操作系统的内存分配问题
- 了解一点大数据(一)
- 手机新闻网站,掌上移动新闻,手机报client,jQuery Mobile手机新闻网站,手机新闻网站demo,新闻阅读器开发
- 用nautilus命令打开文件管理器
- 人类的智慧是一般不多?爱因斯坦的智商是多少?
- qt486中文乱码问题
- ADF中选择表行后改变当前行的编辑状态
- 6月18日课堂心得
- 机器学习傻瓜的深入研究
- xshell传输文件