URI Online Judge - 1168 - Java

Asked

Viewed 1,045 times

3

I am trying to solve the 1168 URI problem. However, I am not getting the number of Leds needed to mount a number. Specifically, my variable is valorLed is always resulting in zero. Can you help me?

Problem: https://www.urionlinejudge.com.br/judge/pt/problems/view/1168

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);
    }
}

Thanks in advance

  • For sure it is because the block inside the if is never executed. By the way, put your code here, it’s good to have a place to test, but you also need to have the code here.

  • jbueno, why the block inside the if is never run, in my case?

  • I don’t know, young man. I put a println in there to test and saw that it was not executed. I even tried to see the reason, but at the moment I am without time.

  • 1

    I suggest using the URI forum (http://www.urionlinejudge.com.br/forum/), someone may already have the same doubt as you at some point.

  • 1

    I think there’s a for and an if left in the code, but without seeing the original problem becomes difficult. The impression is that it would be enough to run all the characters, and adding the respective matrix index corresponding to the number, only this: for i = 0 to tamanho da string, numero = caractere[i] da string convertido, leds += matriz[ numero ][1]

1 answer

4


In one look over I found three errors. I don’t know if they are the only ones.

  1. You’re just taking a single amount v. You should take n values v. I mean, there should be something like this:

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

  3. Use listV[x] will not work because this position will not be keeping the values in the range from 0 to 9 but from 48 to 57 (or 0x30 to 0x39 in hexadecimal), which are the ASCII values of the characters '0' to '9' obtained from processing toCharArray(). A quick way to make it work is by changing listV[x] for (listV[X] - 48).

A hint: your array matriz 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 find out the number of leds needed to form digit 9 like this:

matriz[9]; // número de leds necessário = 6
  • Thank you very much! Doubt solved

Browser other questions tagged

You are not signed in. Login or sign up in order to post.