Sumar y restar vector de una matriz con java
Publicado por pixari (1 intervención) el 02/10/2018 13:38:09
Soy un principiante en el mundo de la programación y estoy tratando de resolver un ejercicio. Pero me he quedado totalmente estancado.
El problema es: se tiene 4 formatos posibles:
Tengo que resolver el? en cada caso, pero teniendo en cuenta que el segundo vector se multiplica por 3, el tercero por 1 y el cuarto por 0 y se resta el último vector.
Me gustaría usar algo parecido a un bucle tipo for, pero me parece imposible, entonces solo uso if / else if. ¿Alguien podría ayudarme?
El problema es: se tiene 4 formatos posibles:
1
2
3
4
5
?<int><int><int><int>
<int>?<int><int><int>
<int><int>?<int><int>
<int><int><int>?<int>
<int><int><int><int>?
Tengo que resolver el? en cada caso, pero teniendo en cuenta que el segundo vector se multiplica por 3, el tercero por 1 y el cuarto por 0 y se resta el último vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class LeaderBoard{
public static void main(String[] args){
String errorFormat = "Format error: You must write 4 integers and one symbol ?";
int[] list = null;
int position = -1, j = 0;
if(args.length!=5){
System.err.println(errorFormat);
}else{
try {
list = new int[4];
for(int i=0;i<args.length;i++){
if(position== -1 && args[i].equals("?")){
position = i;
}else if((position!= -1 && args[i].equals("?")) || (j==4)){
//Hay mas de un ? o no hay ?
System.err.println(errorFormat);
System.exit(1);
}else{
list[j] = Integer.parseInt(args[i]);
j++;
}
}
} catch (NumberFormatException e) {
System.err.println(errorFormat);
System.exit(1);
}
System.out.println("The missing value is "+calculate(position,list));
}
}
static int calculate(int position, int[] list) {
if (position == 0){
positon=list[i]+list[i+1]*3+list[i+2]*1+list[i+3]*0-list[i+4];
} else if (position == 1) {
positon=list[i]+list[i+1]*3+list[i+2]*1+list[i+3]*0-list[i+4];
} else if (position ==2 ) {
positon=list[i]+list[i+1]*3+list[i+2]*1+list[i+3]*0-list[i+4];
} else if (position ==3 ) {
positon=list[i]+list[i+1]*3+list[i+2]*1+list[i+3]*0-list[i+4];
} else if (position == 4) {
positon=list[i]+list[i+1]*3+list[i+2]*1+list[i+3]*0-list[i+4];
}
return 0;
}
Me gustaría usar algo parecido a un bucle tipo for, pero me parece imposible, entonces solo uso if / else if. ¿Alguien podría ayudarme?
Valora esta pregunta
0