UVA11489 - Integer Game(博弈)

题目链接

题意:有一连串的数字,两个人轮流取一个数,当谁取走数后,剩下的数的和不能被3整除,则这个人输了,求出先手是否能胜。

思路:当数只有一个时,先后必胜。当数大于两个时,先判断先手取完数后,剩下的数是否能被3整除,如果可以的话,接下去两个人轮流取的数都必须是3的整数倍。计算3的整数倍的个数就可以了。

代码:

#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;
}
时间: 2024-11-05 18:55:32

UVA11489 - Integer Game(博弈)的相关文章

uva11489 - Integer Game(考思维,找规律)

从S开始时只能是两种情况: 1.现在总和已经是3的倍数了,那么因为每人每次只能拿走一个数,要保持拿走之后依然是3的倍数,那么就只能拿3,6,9这类数,用num统计一下,看看num奇偶性就知道谁最后拿了. 2.现在总和不是3的倍数,那么要么除3余1要么除3余2,就用num1和num2分别统计两种单个数的数目,看S第一下能不能拿完以后变成3的倍数,能就是按第一种情况分析了,不能T就赢了. #include<iostream> #include<cstdio> #include<c

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

poj分类解题报告索引

图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Journey poj1724 - ROADS(邻接表+DFS) BFS poj3278 - Catch That Cow(空间BFS) poj2251 - Dungeon Master(空间BFS) poj3414 - Pots poj1915 - Knight Moves poj3126 - Prim

Integer Game(UVA11489)3的倍数

K - Integer Game Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 11489 题意:在n中取数字,使剩下的数是3的倍数,不能取则失败. 思路:如果能使当前数为3的倍数,数字和必是3的倍数.要保持这种状态,先对每一位对3取余,统计cnt[0],cnt[1],cnt[2]; 的个数,整个串能否被3整除取决于ans=(cnt[1]+cnt[

UVA 11489 Integer Game (博弈)

1227: Integer Game 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

HDU 3980 Paint Chain(博弈 SG)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3980 Problem Description Aekdycoin and abcdxyzk are playing a game. They get a circle chain with some beads. Initially none of the beads is painted. They take turns to paint the chain. In Each turn one p

博弈问题-Alice与Bob拿牌游戏

Description Bob and Alice play a game, and Bob will play first. Here is the rule of the game: 1) There are N stones at first; 2) Bob and Alice take turns to remove stones. Each time, they can remove p^k stones. p is prime number, such as 2, 3, 5, ...

hdu 1907 尼姆博弈

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3745    Accepted Submission(s): 2116 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

HDU 4111 Alice and Bob (博弈)

Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1799    Accepted Submission(s): 650 Problem Description Alice and Bob are very smart guys and they like to play all kinds of games i