UVA 12554 – A Special “Happy Birthday” Song!!!

Interesting problem that is testing my while loop condition writing skills!

import java.io.PrintWriter;
import java.lang.Exception;
import java.util.Scanner;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P12554 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    String[] song = { "Happy", "birthday", "to", "you", "Happy", "birthday", "to", "you", "Happy", "birthday", "to", "Rujia", "Happy", "birthday", "to", "you" };

    int N = sc.nextInt();
    String[] people = new String[N];
    for (int i = 0; i < N; i++) {
      people[i] = sc.next();
    }

    boolean done = false;
    int idxSong = 0;
    int idxPeople = 0;
    while (!done || idxSong != 0) {
      out.println(people[idxPeople] + ": " + song[idxSong]);
      idxPeople++;
      idxSong++;
      if (idxPeople == N) {
        done = true;
        idxPeople = 0;
      }
      if (idxSong == song.length)
        idxSong = 0;
    }

    out.close();
    sc.close();
  }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.