TopCoder 250 points 9-SRM 148 DIV 1 75/250 30%
2013-05-31 15:55
127 查看
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 | ||||||||||||
| ||||||||||||
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) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
| ||||||||||||
4) | ||||||||||||
|
这道题费了有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(); } }
相关文章推荐
- TopCoder 300 points 24-SRM 155 DIV 2 75/250 30%
- TopCoder 250 points 10-SRM 148 DIV 2 167.20/250 66.88%
- 小朋友学TopCoder(7):SRM726 DIV2 250-point
- topcoder SRM 548 DIV2 250
- TopCoder 250 points 11-SRM 149 DIV 1 86.62/250 34.65%
- TopCoder 250 points 16-SRM 151 DIV 2 97.63/250 39.05%
- topcoder SRM 637 div2+div1 250
- TopCoder 250 points 12-SRM 149 DIV 2 103.92/250 41.57%
- TopCoder算法竞赛题4:SRM 148 DIV 2, 250-point
- TopCoder SRM 612 DIV1 250 EmoticonsDiv1
- TopCoder 250 points 28-SRM 157 DIV 2 185.60/250 74.24%
- TopCoder 300 points 1-SRM 144 DIV 1 90/300 30%
- TopCoder 250 points 17-SRM 152 DIV 1 106.51/250 42.60%
- topcoder SRM 543 div2 250
- Topcoder SRM 651 div1 250 题解 (概率dp)
- Topcoder SRM 661 (Div.1) 250 MissingLCM - 数论
- topcoder srm 610 div2 250
- TopCoder 250 points 13-SRM 150 DIV 1 82.63/250 33.05%
- TopCoder 250 points 18-SRM 152 DIV 2 167.05/250 66.82%
- 记第一次TopCoder, 练习SRM 583 div2 250