Question Link
There is a series of numbers. Two people take a number in turn. When the number is taken away, the sum of the remaining number cannot be divided by three. Then the person loses and determines whether the first hand can win.
Train of Thought: when there is only one number, it will win successively. When the number is greater than two, first determine whether the remaining number can be fully divided by three after the number is obtained first. If yes, next, the number of two people in turn must be an integer multiple of 3. Calculate the number of three integers.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 1005;char s[MAXN];int arr[MAXN];int n, sum, used, l;bool judge() { for (int i = 0; i < l; i++) { if ((sum - arr[i]) % 3 == 0) { used = i; return true; } } return false;}int main() { int cas, t = 1; scanf("%d", &cas); while (cas--) { scanf("%s", s); l = strlen(s); sum = 0; for (int i = 0; i < l; i++) { arr[i] = s[i] - '0'; sum += arr[i]; } printf("Case %d: ", t++); if (n == 1) printf("S\n"); else { if (!judge()) printf("T\n"); else { int cnt = 0; for (int i = 0; i < l; i++) { if (i == used) continue; if (arr[i] % 3 == 0) cnt++; } if (cnt % 2 == 0) printf("S\n"); else printf("T\n"); } } } return 0;}
Uva11489-integer game (game)