Created
October 6, 2010 04:22
-
-
Save oskimura/612827 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // BEGIN CUT HERE | |
| // PROBLEM STATEMENT | |
| // NOTE: This problem statement contains images that may not display properly if | |
| // viewed outside of the applet. | |
| /* | |
| * Taro shows a magic trick to Hanako. | |
| * | |
| * | |
| * | |
| * Taro: Hello Hanako. I'll show you a magic trick. Please imagine a positive | |
| * integer less than or equal to 16. Hanako: OK. I imagined it. Taro: (Taro | |
| * shows card 1 to Hanako.) Does this card contain your number? Hanako: Yes. | |
| * Taro: (Taro shows card 2 to Hanako.) Does this card contain your number? | |
| * Hanako: No. Taro: (Taro shows card 3 to Hanako.) Does this card contain your | |
| * number? Hanako: Yes. Taro: (Taro shows card 4 to Hanako.) Does this card | |
| * contain your number? Hanako: Yes. Taro: Your number is 5! | |
| * | |
| * | |
| * | |
| * | |
| * (Card 1 contains 1, 2, 3, 4, 5, 6, 7 and 8. Card 2 contains 1, 2, 3, 4, 9, | |
| * 10, 11 and 12. Card 3 contains 1, 2, 5, 6, 9, 10, 13 and 14. Card 4 contains | |
| * 1, 3, 5, 7, 9, 11, 13 and 15.) | |
| * | |
| * | |
| * Your task is to write a program that simulates this magic trick. You are | |
| * given Hanako's answers in the String answer. The i-th character is 'Y' if she | |
| * answered "yes" to the i-th question, and 'N' if she answered "no" to the i-th | |
| * question. Return the integer Hanako imagined. | |
| * | |
| * DEFINITION Class:NumberMagicEasy Method:theNumber Parameters:String | |
| * Returns:int Method signature:int theNumber(String answer) | |
| * | |
| * | |
| * CONSTRAINTS -answer will contain exactly 4 characters. -Each character in | |
| * answer will be 'Y' or 'N'. | |
| * | |
| * | |
| * EXAMPLES | |
| * | |
| * 0) "YNYY" | |
| * | |
| * Returns: 5 | |
| * | |
| * The example from the statement. | |
| * | |
| * 1) "YNNN" | |
| * | |
| * Returns: 8 | |
| * | |
| * 8 is the only number that exists on the first card and does not exist on any | |
| * other cards. | |
| * | |
| * 2) "NNNN" | |
| * | |
| * Returns: 16 | |
| * | |
| * | |
| * | |
| * 3) "YYYY" | |
| * | |
| * Returns: 1 | |
| * | |
| * | |
| * | |
| * 4) "NYNY" | |
| * | |
| * Returns: 11 | |
| */ | |
| // END CUT HERE | |
| import java.util.ArrayList; | |
| import java.util.HashSet; | |
| import java.util.List; | |
| import java.util.Set; | |
| public class NumberMagicEasy | |
| { | |
| public static void main(final String[] args) | |
| { | |
| final NumberMagicEasy temp = new NumberMagicEasy(); | |
| final String answer = args[0]; | |
| System.out.println(temp.theNumber(answer)); | |
| } | |
| public int theNumber(final String answer) | |
| { | |
| for (int i = 0; i < answer.length(); i++) | |
| { | |
| final Character c = answer.charAt(i); | |
| final List<Integer> card = cards.get(i); | |
| if (c.equals('Y')) | |
| { | |
| final Set<Integer> newAnswerSet = new HashSet<Integer>(); | |
| for (final int n : card) | |
| { | |
| if (answerSet.contains(n)) | |
| { | |
| newAnswerSet.add(n); | |
| } | |
| } | |
| // 答えの集合を更新 | |
| answerSet = newAnswerSet; | |
| } | |
| else | |
| // No | |
| { | |
| for (final int n : card) | |
| { | |
| answerSet.remove(n); | |
| } | |
| } | |
| } | |
| if (answerSet.isEmpty()) | |
| { | |
| return 16; | |
| } | |
| final Integer result = (Integer) answerSet.toArray()[0]; | |
| return result; | |
| } | |
| public NumberMagicEasy() | |
| { | |
| { | |
| c1.add(1); | |
| c1.add(2); | |
| c1.add(3); | |
| c1.add(4); | |
| c1.add(5); | |
| c1.add(6); | |
| c1.add(7); | |
| c1.add(8); | |
| } | |
| { | |
| c2.add(1); | |
| c2.add(2); | |
| c2.add(3); | |
| c2.add(4); | |
| c2.add(9); | |
| c2.add(10); | |
| c2.add(11); | |
| c2.add(12); | |
| } | |
| { | |
| c3.add(1); | |
| c3.add(2); | |
| c3.add(5); | |
| c3.add(6); | |
| c3.add(9); | |
| c3.add(10); | |
| c3.add(13); | |
| c3.add(14); | |
| } | |
| { | |
| c4.add(1); | |
| c4.add(3); | |
| c4.add(5); | |
| c4.add(7); | |
| c4.add(9); | |
| c4.add(11); | |
| c4.add(13); | |
| c4.add(15); | |
| } | |
| { | |
| cards.add(c1); | |
| cards.add(c2); | |
| cards.add(c3); | |
| cards.add(c4); | |
| } | |
| } | |
| Set<Integer> answerSet = new HashSet<Integer>(); | |
| List<Integer> c1 = new ArrayList(); | |
| List<Integer> c2 = new ArrayList(); | |
| List<Integer> c3 = new ArrayList(); | |
| List<Integer> c4 = new ArrayList(); | |
| /* | |
| * (Card 1 contains 1, 2, 3, 4, 5, 6, 7 and 8. Card 2 contains 1, 2, 3, 4, | |
| * 9, 10, 11 and 12. Card 3 contains 1, 2, 5, 6, 9, 10, 13 and 14. Card 4 | |
| * contains 1, 3, 5, 7, 9, 11, 13 and 15.) | |
| */ | |
| List<List<Integer>> cards = new ArrayList<List<Integer>>(); | |
| { | |
| answerSet.add(1); | |
| answerSet.add(2); | |
| answerSet.add(3); | |
| answerSet.add(4); | |
| answerSet.add(5); | |
| answerSet.add(6); | |
| answerSet.add(7); | |
| answerSet.add(8); | |
| answerSet.add(9); | |
| answerSet.add(10); | |
| answerSet.add(11); | |
| answerSet.add(12); | |
| answerSet.add(13); | |
| answerSet.add(14); | |
| answerSet.add(15); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
初参加
書き終えるころにbit演算でやればいいことに気づいた