11489-integer Game
Time limit:1.000 seconds
Http://uva.onlinejudge.org/index.php? OPTION=COM_ONLINEJUDGE&ITEMID=8&CATEGORY=467&PAGE=SHOW_PROBLEM&PROBLEM=24 84
Two players, S and T, are playing a game where they make alternate moves. S plays.
In the game, they start with a integer N. In each move, a player removes one digit from the "the" and passes the resulting number to the other player. The game continues in-fashion until a player finds he/she has no digit to remove while 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 dig its left.
Suppose N = 1234. S has 4 possible moves. That's, 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 the "a" 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 don't contain any zeros.
Output
For each case, output is starting from 1. If s wins then output 's ' otherwise output 'T '.
Sample input Output for sample input
Train of thought: when is s going to win? --n 1 is a situation; n is not 1 o'clock, as long as the first step is to determine whether the number can be taken, from the beginning of the second can only take a multiple of 3, through the number of multiples of 3 of the parity can be obtained results.
Complete code:
/*0.022s*/
#include <cstdio>
#include <cstring>
char ch[1010];
int count[4];
int main (void)
{
int T, I, K;
int n, sum;
scanf ("%d", &t);
for (k = 1; k <= T; k++)
{
scanf ("%s", ch);
n = strlen (ch);
memset (count, 0, sizeof (count));
sum = 0;
for (i = 0; i < n; i++)
{
ch[i] &=;
Count[ch[i]% 3]++;
Sum + + ch[i]% 3;
}
printf ("Case%d:", k);
Puts (n = = 1 | | | sum% 3 = 0 && count[0] & 1 | | sum% 3 &&
count[sum% 3] && (count[0) &am P 1) = = 0? "S": "T");
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/