Advertisement
javipinero

The Other Clock Java Tuenti Contest

Jun 21st, 2011
1,483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. package theClock;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5.  
  6. public class TheClock {
  7.  
  8.     private Integer cuantosEn59[];
  9.     private Integer numLed[];
  10.     private Integer totalDisplayados[];
  11.     private int[] tiempo; // Variable para almacenar el tiempo
  12.     private int cerosDecenas;
  13.     private int numledDecenas;
  14.  
  15.     public TheClock(int segundos) {
  16.  
  17.         tiempo = new int[3];
  18.         numLed = new Integer[10];
  19.         numLed[0] = 1;
  20.         numLed[1] = 0;
  21.         numLed[2] = 4;
  22.         numLed[3] = 1;
  23.         numLed[4] = 1;
  24.         numLed[5] = 2;
  25.         numLed[6] = 1;
  26.         numLed[7] = 1;
  27.         numLed[8] = 4;
  28.         numLed[9] = 0;
  29.         numledDecenas = 2;
  30.  
  31.         cuantosEn59 = new Integer[10];
  32.  
  33.         for (int i = 0; i < 10; i++) {
  34.             cuantosEn59[i] = cuantosAparecen(i, 59);
  35.         }
  36.  
  37.         intToTime(segundos);
  38.         totalDisplayados = new Integer[9 + 1];
  39.         for (int i = 0; i < 10; i++) {
  40.             int auxDisplay;
  41.             auxDisplay = contarDigitos(i, tiempo, 60, cuantosEn59[i]);
  42.             totalDisplayados[i] = auxDisplay;
  43.         }
  44.         totalDisplayados[0] -= 3;
  45.         cerosDecenas = tiempo[0] + tiempo[0] * 59 + tiempo[1];
  46.  
  47.     }
  48.  
  49.     /**
  50.      * Obtiene el total de led's que hemos displayado
  51.      *
  52.      * @return el n�mero de led's displayados
  53.      */
  54.     public int getTotalLedDisplayados() {
  55.         int devolver = 0;
  56.         for (int i = 0; i < 10; i++) {
  57.             devolver += totalDisplayados[i] * numLed[i];
  58.         }
  59.         devolver += cerosDecenas * numledDecenas;
  60.         return devolver + 36;
  61.     }
  62.  
  63.     private void intToTime(int segundos) {
  64.         tiempo[0] = segundos / 3600;
  65.         segundos %= 3600;
  66.         tiempo[1] = segundos / 60;
  67.         segundos %= 60;
  68.         tiempo[2] = segundos;
  69.     }
  70.  
  71.     public static int contarDigitos(int value, int array[], int base,
  72.             int numAparicionesBase) {
  73.  
  74.         int devolver = 0;
  75.  
  76.         for (int i = 0; i < array.length; i++) {
  77.  
  78.             devolver += (antes(array, i, base)) * numAparicionesBase
  79.                     + cuantosAparecen(value, array[i]);
  80.         }
  81.         return devolver;
  82.  
  83.     }
  84.  
  85.     public static int antes(int array[], int indice, int base) {
  86.  
  87.         int resultado = 0;
  88.         for (int i = 0; i < indice; i++) {
  89.             resultado += array[i] * Math.pow(base, indice - i - 1);
  90.         }
  91.         return resultado;
  92.     }
  93.  
  94.     public static int cuantasCifras(int numero) {
  95.         int aux = 0;
  96.  
  97.         while (numero > 0) {
  98.             numero /= 10;
  99.             aux++;
  100.         }
  101.  
  102.         return aux;
  103.     }
  104.  
  105.     public static int cuantosAparecen(int buscado, int valor) {
  106.  
  107.         if (buscado == 0) {
  108.             return valor / 10 + 1;
  109.         }
  110.  
  111.         int delante;
  112.         int actual;
  113.         int numCifras;
  114.         int devolver = 0;
  115.  
  116.         numCifras = cuantasCifras(valor);
  117.         for (int i = numCifras - 1; i >= 0; i--) {
  118.             int aux = (int) Math.pow(10, i);
  119.             int aparicionesPosicionActual = 0;
  120.             delante = valor / aux;
  121.             actual = delante % 10;
  122.             delante /= 10;
  123.             if (actual >= buscado) {
  124.                 aparicionesPosicionActual = (delante + 1);
  125.             } else {
  126.                 aparicionesPosicionActual = delante;
  127.             }
  128.             devolver += aparicionesPosicionActual;
  129.         }
  130.         return devolver;
  131.     }
  132.  
  133.     public static void main(String[] args) {
  134.  
  135.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  136.         try {
  137.  
  138.             while (br.ready()) {
  139.                 String linea = br.readLine();
  140.                 TheClock theClock = new TheClock(Integer.parseInt(linea));
  141.                 System.out.println(theClock.getTotalLedDisplayados());
  142.             }
  143.  
  144.         } catch (Exception e) {
  145.             e.printStackTrace();
  146.         }
  147.  
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement