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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


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=2484


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?

InputThe 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 NN has at most 1000 digits and does not contain any zeros.

OutputFor each case, output the case number starting from 1. If S wins then output ‘S’ otherwise output ‘T’.

Sample Input      Output for Sample Input

3
4
33
771                             

Case 1: S
Case 2: T
Case 3: T                                        


思路: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;
}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

“UVa 11489 Integer Game (博弈&想法题)” 的相关文章

Python实用操作有哪些 - 开发技术

这篇文章主要介绍“Python实用操作有哪些”,在日常操作中,相信很多人在Python实用操作有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python实用操作有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!1)映射代理(...

mongo的tickets被耗尽导致卡顿问题怎么解决 - 开发技术

这篇文章主要介绍了mongo的tickets被耗尽导致卡顿问题怎么解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇mongo的tickets被耗尽导致卡顿问题怎么解决文章都会有所收获,下面我们一起来看看吧。tickets是什么为了解决这...

【SQL开发实战技巧】系列(十一):拿几个案例讲讲translate|regexp

系列文章目录 【SQL开发实战技巧】系列(一):关于SQL不得不说的那些事 【SQL开发实战技巧】系列(二):简单单表查询 【SQL开发实战技巧】系列(三):SQL排序的那些事 【SQL...

select函数

#include<sys/select.h> #include<sys/time.h> int select(int maxfdp1,fd_set *readset,fd_set *writeset,fd_set *exceptset...

【Office】Microsoft Office下载地址合集(微软官方原版离线安装下载)

一、Office2021下载 1、 专业增强版强烈推荐 http://officecdn.microsoft.com/pr/492350f6-3a01-4f97-b9c0-c7c6ddf67d60/media/zh-cn/ProPlus2021Retail.img 2、 专业版 http://o...

HDU 1061 Rightmost Digit (数学&三种解法)

Rightmost Digit http://acm.hdu.edu.cn/showproblem.php?pid=1061 Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/32...