URI Online Judge - 1168 - Java

2

I'm trying to solve URI problem 1168. However, I can not get the number of leds needed to mount a number. Specifically, my valorLed variable is always resulting in zero. Can you help me?

Problem: link

My code:

import java.util.Scanner;
public class Main {
    public static void main (String []args){
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        String v = sc.next();

        /* matriz faz a correspondencia entre um algarismo e a 
        quantidade de led necessaria p/ monta-lo */
        char [][] matriz = {{0,6}, {1,2}, {2,5}, {3,5}, {4,4}, {5,5}, {6,6}, {7,3}, {8,7}, {9,6}};
        char[] listV = v.toCharArray();

        //valorLed eh a variavel responsavel por armazenar o numero de leds necessarios
        int valorLed = 0;

        for(int linha = 0; linha < 9; linha++){
            for(int x = 0; x < listV.length; x++ ){
                if(matriz[linha][0] == listV[x]){
                    valorLed += (int) matriz[linha][1];
                }
            }
        }
        System.out.print(valorLed);
    }
}

Thank you in advance

    
asked by anonymous 26.06.2016 / 21:36

1 answer

3

At a glance I found three errors. I do not know if they are the only ones.

  • You're only getting a single value v . You should get n values v . That is, it should look something like this:

    int n = sc.nextInt();
    for (int i = 0; i < n; i++) {
        String v = sc.next();
        processarValor(v);
    }
    
  • Your first for is going to linha < 9 . I think I should go to linha <= 9 .

  • Using listV[x] will not work because this position will not be storing values in the range of 0 to 9, but 48 to 57 (or 0x30 to 0x39 in hexadecimal), which are the ASCII values of '0' a '9' obtained with the toCharArray() transformation. A quick way to get it to work is by switching listV[x] to (listV[X] - 48) .

  • One tip: your matriz array does not have to be two-dimensional. The digits 0 to 9 already correspond to the indexes of a normal one-dimensional array. So you could have something like this:

    char [] matriz = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
    

    And you could figure out the number of leds needed to form digit 9 like this:

    matriz[9]; // número de leds necessário = 6
    
        
    26.06.2016 / 22:41