Question (in Chinese): http://ac.jobdu.com/problem.php?pid=1355
Question Name: Continuous Cards
Question Description: For a set of cards, Ace is treated as 1. J is 11, Q 12, and K 13. Jokers, up to four jokers, is represented as 0, and could be used as any card OR not used at all. Given some cards, check whether they are a consecutive sequence, such as 1 2 3 4 5.
Input: the input might contain multiple test cases. In each test case, the first line is an integer N (0 <= N <= 14). If N is 0, quite the program. Otherwise, the next line contains N integers as the content of the cards.
Output: if the cards could be a sequence, print “So Lucky!”; or print “Oh My God!” otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Input: 5 3 5 1 0 4 5 3 5 4 7 6 5 3 5 7 4 8 14 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Output: So Lucky! So Lucky! Oh My God! So Lucky! |
The solution is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | /*---------------------------------------------------------------- * Author: Sheng Yu - codesays.com * Written: 09/11/2014 * Last updated: 09/11/2014 * * Filename: ContinuousCards.java * Compilation: javac ContinuousCards.java * Execution: java ContinuousCards * JDK Used: 1.7 *----------------------------------------------------------------*/ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; /** * The utility to determine whether the integer array is a consecutive sequence, * if 0 could be used as any value. * @author Sheng Yu codesays.com */ public class ContinuousCards { /** * If 0 could be used as any card, determine whether the cards is a consecutive * sequence. * @param cards an integer array * @return true if the input cards are a sequence; false otherwise. */ public static boolean isContinuousCards(int[] cards) { // Count the occurrence of the cards. int[] count = new int[14]; for (int card : cards) { if (card != 0 && count[card] == 1) { return false; // Two same cards } count[card] += 1; } // Skip the non-existing cards in head and tail. int begin = 1, end = 13; while (begin < 14 && count[begin] == 0) ++begin; while (end > 0 && count[end] == 0) --end; // Count the gap between the smallest and greatest cards. int gap = 0; while (++begin < end) { if (count[begin] == 0) ++gap; } // Check if we can fill the gap with our "0" cards. return gap <= count[0]; } /** * Stub for test. * @param args the command line arguments. * @throws IOException when input gets error. */ public static void main(String[] args) throws IOException { StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); int size = 0; int[] cards = null; while (st.nextToken() != StreamTokenizer.TT_EOF) { size = (int) st.nval; if (size == 0) break; cards = new int[size]; while (size-- > 0) { st.nextToken(); cards[size] = (int) st.nval; } if (isContinuousCards(cards)) System.out.println("So Lucky!"); else System.out.println("Oh My God!"); } } } |