UVa11489 - Integer Game(博弈)

Two players, S and T, are playing a game where they makealternate moves.S plays first.
In this game, they start with an integer N. In each move, a player removesone 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 toremove when that player is declared as the loser.

With this restriction, it’s obvious that if the number of digits in Nis odd thenS wins otherwise T wins. To make the game moreinteresting, we apply one additional constraint. A player can remove aparticular digit if the sum of digits of the resulting number is a multiple of3 or there are no digits left.

Suppose N = 1234. S has 4 possible moves. That is, he canremove 1, 2, 3, or 4.  Of these, two ofthem are valid moves.

- Removal of 4 results in 123 and the sum of digits = 1 + 2 + 3 = 6; 6 isa multiple of 3.
- Removal of 1 results in 234 and the sum of digits = 2 + 3 + 4 = 9; 9 is amultiple of 3.
The other two moves are invalid.

If both players play perfectly, who wins?

Input
Thefirst line of input is an integer T(T<60) thatdetermines the number of test cases. Each case is a line that contains apositive integerN. N has at most 1000 digits and does notcontain any zeros.

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

Sample Input                             Output forSample Input

3
4
33
771

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

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main {
	public static final boolean DEBUG = false;
	public StreamTokenizer tokenizer;
	public BufferedReader cin;
	public PrintWriter cout;
	public String s;
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
			tokenizer = new StreamTokenizer(cin);
			tokenizer.resetSyntax();
			tokenizer.wordChars('0', '9');
			tokenizer.whitespaceChars(0, ' ');
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String next() 
	{
		try {
			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval; 
			else return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String input()
	{
		try {
			return s = next();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}	
	public void solve(int cas) 
	{
		int[] cnt = new int[3];
		int sum = 0;
		
		for (int i = 0, len = s.length(); i < len; i++) {
			char ch = s.charAt(i);
			cnt[ch % 3]++;
			sum += ch - '0';
		}
		
		int step = 0;
		if (cnt[sum % 3] != 0) {
			step = 1;
			cnt[sum % 3]--;
		}
		
		if (step == 1) step += cnt[0];
		
		cout.println("Case " + cas + ": " + ((step % 2 == 1) ? "S" : "T"));
		cout.flush();
		
	}

	public static void main(String[] args) {
		Main solver = new Main();
		solver.init();
		
		int t = (int)Double.parseDouble(solver.next());
		for (int i = 1; i <= t; i++) {
			solver.input();
			solver.solve(i);
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值