Advertisement
javipinero

The Clock Java Tuenti Contest

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