UVa 11489 Integer Game (博弈&想法題)

來源:互聯網
上載者:User

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 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, 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 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      Output for Sample Input

思路:S何時會贏?——n為1是一種情況;n不為1時,只要判斷第一步能否取數即可, 從第二步開始就只能取3的倍數了,通過3的倍數的個數的奇偶性就可求得結果。

完整代碼:

/*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] &= 15;            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] & 1) == 0 ? "S" : "T");    }    return 0;}

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.