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.
– Jéf Bueno
jbueno, why the block inside the if is never run, in my case?
– Kfcaio
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.– Jéf Bueno
I suggest using the URI forum (http://www.urionlinejudge.com.br/forum/), someone may already have the same doubt as you at some point.
– pmargreff
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]
– Bacco