TopCoder 250 points 9-SRM 148 DIV 1 75/250 30%

Problem Statement

 

Tommy is learning a simple card game called Circle. To play the game, the single player shuffles a deck of cards. He or she then flips through the deck, removing all instances of the 'K' card, and all consecutive pairs of cards that add up to 13. The deck does wrap around, so that if the last card remaining in the deck and the first card remaining in the deck add up to 13, they are both removed. The player keeps cycling through the deck until no more cards can be removed.

Create a class CircleGame containing the method cardsLeft that takes a String deck representing a not-necessarily complete nor correct deck of cards. Each character ofdeck represents the value of a card without the suit. The values shown on the card represent the following numerical values:

'A' - 1
'2' - 2
'3' - 3
'4' - 4
'5' - 5
'6' - 6
'7' - 7
'8' - 8
'9' - 9
'T' - 10
'J' - 11
'Q' - 12
'K' - 13

Given deck, return the number of cards remaining after the game has been played to its fullest such that no more cards can be removed.

Definition

 
Class:CircleGame
Method:cardsLeft
Parameters:String
Returns:int
Method signature:int cardsLeft(String deck)
(be sure your method is public)
 
 

Notes

-There are no guarantees made about the contents of the deck. For example, there can be more than four of a given value of card.
-Note that if a particular card can make a sum of 13 with the card before or after it, it does not matter which is chosen. For example, 38582, whether you use the first 8 or the second 8, will still become 382 after the two cards (5 and 8) are removed.

Constraints

-deck will have between 10 and 50 characters, inclusive.
-each character of deck will be a character from the set {'2'-'9','A','T','J','Q','K'}.

Examples

0) 
 
"KKKKKKKKKK"
Returns: 0
All K cards are always removed from the deck.
1) 
 
"KKKKKAQT23"
Returns: 1
The K cards are removed, leaving AQT23. AQ are then removed because they add up to 13, leaving T23. Since the deck wraps around and T and 3 add up to 13, they are also removed, just leaving the 2.
2) 
 
"KKKKATQ23J"
Returns: 6
Only the K cards can be removed.
3) 
 
"AT68482AK6875QJ5K9573Q"
Returns: 4
The remaining deck is 6875.
4) 
 
"AQK262362TKKAQ6262437892KTTJA332"
Returns: 24
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     

这道题费了有4个小时,,,思路不清,,全靠改bug改出来的,,系统测试不断有错误,,回头好好整理一下,为了做这题,中饭也没吃,,,


import java.util.HashMap;
import java.util.LinkedList;

public class CircleGame {
	public final static HashMap<Character, Integer> m = new HashMap<Character, Integer>();

	static {
		m.put('A', 1);
		m.put('2', 2);
		m.put('3', 3);
		m.put('4', 4);
		m.put('5', 5);
		m.put('6', 6);
		m.put('7', 7);
		m.put('8', 8);
		m.put('9', 9);
		m.put('T', 10);
		m.put('J', 11);
		m.put('Q', 12);
		m.put('K', 13);
	}



	public  int cardsLeft(String deck) {
		LinkedList<Character> list = new LinkedList<Character>();
		int compareA = 0;
		int compareB = 0;
		int indexA = 0;
		int indexB = indexA + 1;
		boolean sumFirstLast = false;
		int hasDeletedtimes = 0;
		char carray[] = deck.toCharArray();
		int len = carray.length;
		for (int i = 0; i < len; i++)
			if (carray[i] != 'K')
				list.add(carray[i]);

		do {
			hasDeletedtimes = 0;
			len = list.size();
			if (len == 0)
				return 0;
			else if (len == 1)
				return 1;
			else if (len == 2) {
				if (m.get(list.get(0)) + m.get(list.get(1)) == 13)
					return 0;
				else
					return 2;
			} else {
				if (!sumFirstLast) {
					sumFirstLast = true;
					compareA = m.get(list.getFirst());
					compareB = m.get(list.getLast());
					if (compareA + compareB == 13) {
						list.removeFirst();
						list.removeLast();
						hasDeletedtimes++;
						sumFirstLast = false;
					}
				} else {
					sumFirstLast = false;
					while (true) {
						compareA = m.get(list.get(indexA));
						compareB = m.get(list.get(indexB));
						if (indexB == len - 1) {
							if (compareA + compareB == 13) {
								list.remove(indexA);
								list.remove(indexA);
								hasDeletedtimes++;
							}
							break;
						} else {
							if (compareA + compareB == 13) {
								list.remove(indexA);
								list.remove(indexA);
								hasDeletedtimes++;
								len = list.size();
								if (indexA == len - 1)
									break;
								else
									continue;
							} else {
								indexA++;
								indexB++;
								continue;
							}
						}
					}
					indexA = 0;
					indexB = 1;
				}

			}
			if (hasDeletedtimes == 0 && !sumFirstLast)
				break;

		} while (true);

		return list.size();
	}
}


估计30%的分数是最低了,,后来又想了种稍微简单一点的代码

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

public class CircleGame {
	public final static HashMap<Character, Integer> m = new HashMap<Character, Integer>();

	static {
		m.put('A', 1);
		m.put('2', 2);
		m.put('3', 3);
		m.put('4', 4);
		m.put('5', 5);
		m.put('6', 6);
		m.put('7', 7);
		m.put('8', 8);
		m.put('9', 9);
		m.put('T', 10);
		m.put('J', 11);
		m.put('Q', 12);
		m.put('K', 13);
	}



	public  int cardsLeft(String deck) {
		LinkedList<Character> list = new LinkedList<Character>();
		int compareA = 0;
		int compareB = 0;
		int hasDeletedtimes = 0;
		char carray[] = deck.toCharArray();
		int len = carray.length;
		for (int i = 0; i < len; i++)
			if (carray[i] != 'K')
				list.add(carray[i]);

		do {
			hasDeletedtimes = 0;
			len = list.size();
			if (len == 0)
				return 0;
			else if (len == 1)
				return 1;
			else if (len == 2) {
				if (m.get(list.get(0)) + m.get(list.get(1)) == 13)
					return 0;
				else
					return 2;
			} else {
				for (int i = 0; i < len; i++) {
					compareA = m.get(list.get(i));
					compareB = m.get(list.get(i + 1));
					if (compareA + compareB == 13) {
						list.set(i, 'K');
						list.set(i + 1, 'K');
						hasDeletedtimes++;
						if (i == len - 2 || i == len - 3) {
							break;
						}
						i++;

					} else {
						if (i == len - 2) {
							break;
						}
					}
				}

				for (Iterator<Character> iter = list.iterator(); iter.hasNext();)
					if (iter.next() == 'K')
						iter.remove();
				if (m.get(list.getFirst()) + m.get(list.getLast()) == 13) {
					list.removeFirst();
					list.removeLast();
					hasDeletedtimes++;
				}
			}
			if (hasDeletedtimes == 0)
				break;

		} while (true);

		return list.size();
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值