UVA - 11489
题目链接https://vjudge.net/contest/321024#problem/J
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
题目大意:给你一个数字串n,两个人从中轮流取出一些数字,要求每次取完之后剩下的数是3的倍数,不能取数者输。先手胜输出S,否则输出T。
。。。题目的数据比较小,可以直接暴力枚举取走每一个数字之后的结果。对于有多个数满足取完之后仍然为3的倍数时我们取走最大的那个,这样可以是得下一个人的取数范围缩小,比较有利于形式。至于如何判断是3的倍数。。。。数字相加为3的倍数即可;
以下是AC代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 1e6;
const int mac = 1e4 + 10;
const int inf = 1e8 + 10;
char s[1050];
int vis[1050];
int main()
{
int t, n, m, cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%s", s);
memset(vis, 0, sizeof vis);
printf("Case %d: ", cas++);
int len = strlen(s);
if (len == 1) { printf("S\n"); continue; }
int sum = 0;
for (int i = 0; i < len; i++) sum += s[i] - '0';
int flag = 0;
while (1) {
int mark = 0, p = -1, id;
for (int i = 0; i < len; i++) {
if (!vis[i]) {
if ((sum - (s[i] - '0')) % 3 == 0) {
mark = 1;
if (s[i] - '0' > p) {
p = s[i] - '0'; id = i;
}
}
}
}
if (!mark) break;
else { flag ^= 1; vis[id] = 1; sum -= s[id] - '0'; }
}
if (flag == 0) printf("T\n");
else printf("S\n");
}
return 0;
}