Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package theClock;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class TheClock {
- private Integer cuantosEn59[];
- private Integer numLed[];
- private Integer totalDisplayados[];
- private int[] tiempo; //Variable para almacenar el tiempo
- private int numPasos;
- public TheClock(int segundos) {
- tiempo = new int[3];
- numLed = new Integer[9 + 1];
- numLed[0] = 6;
- numLed[1] = 2;
- numLed[2] = 5;
- numLed[3] = 5;
- numLed[4] = 4;
- numLed[5] = 5;
- numLed[6] = 6;
- numLed[7] = 3;
- numLed[8] = 7;
- numLed[9] = 6;
- cuantosEn59 = new Integer[9];
- for (int i = 0; i < 9; i++) {
- cuantosEn59[i] = cuantosAparecen(i + 1, 59);
- }
- intToTime(segundos);
- totalDisplayados = new Integer[9+1];
- totalDisplayados[0] = 0;
- for (int i = 1; i < 10; i++) {
- int auxDisplay;
- auxDisplay = contarDigitos(i, tiempo, 60, cuantosEn59[i-1]);
- totalDisplayados[0] += auxDisplay;
- totalDisplayados[i] = auxDisplay;
- }
- totalDisplayados[0] = getDigitosDisplayados() - totalDisplayados[0];
- }
- /**Obtiene el total de led's que hemos displayado
- *
- * @return el número de led's displayados
- */
- public int getTotalLedDisplayados() {
- int devolver = 0;
- for (int i = 0; i < 10; i++) {
- devolver += totalDisplayados[i] * numLed[i];
- }
- return devolver;
- }
- private void intToTime(int segundos) {
- numPasos = segundos + 1;
- tiempo[0] = segundos / 3600;
- segundos %= 3600;
- tiempo[1] = segundos / 60;
- segundos %= 60;
- tiempo[2] = segundos;
- }
- private int getDigitosDisplayados() {
- return numPasos * 6;
- }
- public static int contarDigitos(int value, int array[], int base, int numAparicionesBase) {
- int devolver = 0;
- for (int i=0; i<array.length; i++) {
- 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);
- }
- return devolver;
- }
- public static int antes(int array[], int indice, int base) {
- int resultado = 0;
- for (int i = 0; i < indice; i++) {
- resultado += array[i] * Math.pow(base, indice-i-1);
- }
- return resultado;
- }
- public static int despues(int array[], int indice, int base) {
- int resultado = 0;
- for (int i = indice + 1; i < array.length; i++) {
- resultado += array[i] * Math.pow(base, array.length-i-1);
- }
- return resultado;
- }
- public static int numApariciones(int value, int numero) {
- int resultado = 0;
- while (numero != 0) {
- if (numero%10 == value) {
- resultado++;
- }
- numero /= 10;
- }
- return resultado;
- }
- public static int cuantasCifras(int numero) {
- int aux = 0;
- while (numero > 0) {
- numero /= 10;
- aux++;
- }
- return aux;
- }
- public static int cuantosAparecen(int buscado, int valor) {
- int delante;
- int detras;
- int actual;
- int numCifras;
- int devolver = 0;
- numCifras = cuantasCifras(valor);
- for (int i = numCifras - 1; i >= 0; i--) {
- int aux = (int) Math.pow(10, i);
- int aparicionesPosicionActual = 0;
- delante = valor / aux;
- actual = delante % 10;
- delante /= 10;
- detras = valor % aux;
- if (actual == buscado) {
- aparicionesPosicionActual = delante * aux + detras + 1;
- } else if (actual > buscado) {
- aparicionesPosicionActual = (delante + 1) * aux;
- } else {
- aparicionesPosicionActual = delante * aux;
- }
- devolver += aparicionesPosicionActual;
- }
- return devolver;
- }
- public static void main(String[] args) {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- try {
- while (br.ready()) {
- String linea = br.readLine();
- TheClock theClock = new TheClock(Integer.parseInt(linea));
- System.out.println(theClock.getTotalLedDisplayados());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement