11/08/2013

12554 - A Special "Happy Birthday" Song!!! - Java

 import java.io.BufferedReader;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.IOException;  
 import java.io.InputStream;  
 import java.io.InputStreamReader;  
 import java.util.StringTokenizer;  
 public class HappyBirthday {  
      static String[] song = {"Happy", "birthday", "to", "you",   
                                    "Happy", "birthday", "to", "you",   
                                    "Happy", "birthday", "to", "Rujia",  
                                    "Happy", "birthday", "to", "you"  
      };  
      public static void main(String[] args) throws Exception {  
           Reader.init(System.in);  
 //          Reader.init(new FileInputStream(new File("input.in")));  
           int noOfPeople = Reader.nextInt();  
           String[] peopleNames = new String[noOfPeople];  
           for(int i = 0; i < noOfPeople; i++) {  
                peopleNames[i] = Reader.next();  
           }  
           boolean isDone = false;  
           int i = 0;  
 //          System.out.println("DEBUG: song lenght: " + song.length);  
 //          System.out.println("DEBUG: noOfpeopne: " + noOfPeople);  
           while(!isDone) {  
                System.out.println(peopleNames[i%noOfPeople] + ": " + song[i%16]);  
                if(i%16 == 15 && i >= noOfPeople) isDone = true;  
                i++;  
 //               System.out.println("*****DEBUG: song lenght: " + song.length);  
 //               System.out.println("*****DEBUG: noOfpeopne: " + noOfPeople);  
 //               System.out.println("******DEBUG Current song index: " + i%song.length + " current i:" + i);  
           }  
      }  
 }  
 /** Class for buffered reading int and double values */  
 /** http://www.cpe.ku.ac.th/~jim/java-io.html */  
 class Reader {  
      static BufferedReader reader;  
      static StringTokenizer tokenizer;  
      /** call this method to initialize reader for InputStream */  
      static void init(InputStream input) {  
           reader = new BufferedReader(new InputStreamReader(input));  
           tokenizer = new StringTokenizer("");  
      }  
      /** get next word */  
      static String next() throws IOException {  
           while ( ! tokenizer.hasMoreTokens() ) {  
                //TODO add check for eof if necessary  
                tokenizer = new StringTokenizer(reader.readLine());  
           }  
           return tokenizer.nextToken();  
      }  
      static int nextInt() throws IOException {  
           return Integer.parseInt( next() );  
      }  
      static double nextDouble() throws IOException {  
           return Double.parseDouble( next() );  
      }  
 }  

1 comment:

  1. A programmer puts two glasses on his bedside table before going to sleep. A full one, in case he gets thirsty, and an empty one, in case he doesn't.

    ReplyDelete