Advertisement
alefhidalgo

TheClock

Jun 20th, 2011
1,769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Tuenti Programming Contest
  5.  * Challenge 6: The clock
  6.  *
  7.  * @author alefhidalgo [at] gmail [dot] com
  8.  */
  9. public class TheClock {
  10.  
  11.     public int led[];
  12.     public int[] ledMap;
  13.     {
  14.         ledMap = new int[10];
  15.         ledMap[0] = 6;
  16.         ledMap[1] = 2;
  17.         ledMap[2] = 5;
  18.         ledMap[3] = 5;
  19.         ledMap[4] = 4;
  20.         ledMap[5] = 5;
  21.         ledMap[6] = 6;
  22.         ledMap[7] = 3;
  23.         ledMap[8] = 7;
  24.         ledMap[9] = 6;
  25.         led = new int[6];
  26.     }
  27.  
  28.     private int getLeds() {
  29.         int sum = 0;
  30.         for (int d : led) {
  31.             sum += ledMap[d];
  32.         }
  33.         return sum;
  34.     }
  35.  
  36.     private void setTime(int h, int m, int s) {
  37.         String shours = String.valueOf(h);
  38.         String smin = String.valueOf(m);
  39.         String ssec = String.valueOf(s);
  40.         if (shours.length() > 1) {
  41.             led[4] = Integer.parseInt(shours.substring(1, 2));
  42.             led[5] = Integer.parseInt(shours.substring(0, 1));
  43.         } else {
  44.             led[4] = Integer.parseInt(shours.substring(0, 1));
  45.             led[5] = 0;
  46.         }
  47.  
  48.         if (smin.length() > 1) {
  49.             led[2] = Integer.parseInt(smin.substring(1, 2));
  50.             led[3] = Integer.parseInt(smin.substring(0, 1));
  51.         } else {
  52.             led[2] = Integer.parseInt(smin.substring(0, 1));
  53.             led[3] = 0;
  54.         }
  55.  
  56.         if (ssec.length() > 1) {
  57.             led[0] = Integer.parseInt(ssec.substring(1, 2));
  58.             led[1] = Integer.parseInt(ssec.substring(0, 1));
  59.         } else {
  60.             led[0] = Integer.parseInt(ssec.substring(0, 1));
  61.             led[1] = 0;
  62.         }
  63.     }
  64.  
  65.     public int getTotalLeds(int segs) {
  66.         int sum = 0;
  67.         for (int i = 0; i <= segs; i++) {
  68.             int seg = i % 60;
  69.             int min = (i / 60) % 60;
  70.             int hours = (i / 3600) % 12;
  71.             setTime(hours, min, seg);
  72.             sum += getLeds();
  73.         }
  74.         return sum;
  75.     }
  76.  
  77.     public static void main(String args[]) {
  78.         TheClock theClock = new TheClock();
  79.         Scanner in = new Scanner(System.in);
  80.         while (in.hasNextInt()) {
  81.             System.out.println(theClock.getTotalLeds(in.nextInt()));
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement