I have this sequence of numbers: 7, 14, 21, 28, 35, 42, 49
, being able to have more or less numbers.
I need to add them all as follows:
7+7=14
14+7=21
21+7=28
28+7=35
35+7=42
42+7=49
49+7=56
That was 7, the others are missing. All + 14, all + 21, all + 28, so on ...
Example:
7=(14,21,28,35,42,49,56)
14=(21,28,35,42,49,56,63)
21=(28,35,42,49,56,63,70)
28=(35,42,49,56,63,70,77)
35=(42,49,56,63,70,77,84)
42=(49,56,63,70,77,84,91)
49=(56,63,70,77,84,91,98)
These are the results of each sum of all numbers, and the number that appeared most often is "56" ie:
7+49=56
14+42=56
21+35=56
28+28=56
35+21=56
42+12=56
49+7=56
I need to print these results in lists and check the number of numbers to get the number that most appears in the results.
Print Results:
7=(14,21,28,35,42,49,56)
14=(21,28,35,42,49,56,63)
21=(28,35,42,49,56,63,70)
28=(35,42,49,56,63,70,77)
35=(42,49,56,63,70,77,84)
42=(49,56,63,70,77,84,91)
49=(56,63,70,77,84,91,98)
Print Quantity Check:
14=1
21=2
28=3
35=4
42=5
49=6
56=7*
63=6
70=5
77=4
84=3
91=2
98=1
Print Final Result:
The sum with the highest result is the number "56".
I tried this code but it is not working:
public static void main(String[] args) {
for (long a=1; a <= 7; a++) {
long z;
z=a*7;
for (long x = z; x <= z; x++) {
long y;
y=z+x;
System.out.println(x+"="+y);
}
}
}
}
Program exit:
Theoutputoftheprogramwouldhavetogolikethis:
Lists:
7=(14,21,28,35,42,49,56)14=(21,28,35,42,49,56,63)21=(28,35,42,49,56,63,70)28=(35,42,49,56,63,70,77)35=(42,49,56,63,70,77,84)42=(49,56,63,70,77,84,91)49=(56,63,70,77,84,91,98)
Verification:
14=121=228=335=442=549=656=7*63=670=577=484=391=298=1
Result:
56*
UPDATE
Myprogramsumsallnumbersinagiven"sequence" with the sequence of numbers themselves, and prints a list of the results of each sequence. The implementation of the program would be to verify the results of all the sums, that is, to verify the quantity of each result and to print the amount of times that this result was obtained, thus determining, what was the result that appeared more often. p>
The program is divided into 3 stages:
Step 1 is complete:
Sequence established: 7,14,21,28,35,42,49
.
1) Lists
7=(14,21,28,35,42,49,56)
14=(21,28,35,42,49,56,63)
21=(28,35,42,49,56,63,70)
28=(35,42,49,56,63,70,77)
35=(42,49,56,63,70,77,84)
42=(49,56,63,70,77,84,91)
49=(56,63,70,77,84,91,98)
In Step 2, I'm having some difficulties, I'm not able to pass Lista
that is in Integer
to int
, an error occurs in this conversion and does not work verification.
In program% w / o% was to be% w /% I left% w /% w which are only sequence numbers, (7,14,21,28,35,42,49) as an example only, because I get the conversion for the verification to work.
Program:
package etapa2;
import java.util.ArrayList;
import java.util.List;
public class Etapa2 {
private List<Integer> lista = null;
public Etapa2(List<Integer> lista) {
this.lista = lista;
}
public void soma() {
for (Integer externo : lista) {
System.out.print(externo + "=");
List<String> resultado = new ArrayList<String>();
for (Integer interno : lista) {
resultado.add(String.valueOf(interno + externo));
}
System.out.println("("+String.join(",", resultado)+")");
}
}
public static void main(String args[]) {
List<Integer> lista = new ArrayList<>();
int[] check = new int[100000000];
for (int f=1; f<=7; f++){
int z,p;
z=f*7;
lista.add(z);
check[z]++;
}
Etapa2 sl = new Etapa2(lista);
sl.soma();
System.out.println("------------");
int maior = 0;
for (int i : check) {
if (i > maior) maior = i;
}
for (int j = 0; j <= 1000000; j++) {
if (check[j] == 0) continue;
System.out.println(j + "=" + check[j] + (check[j] == maior ? "*" : ""));
}
}
}
Output:
Wehaveseenthatthe2ndStepiswrong,becausethe"list" to be checked is not placed, but the sequence numbers are just an example.
With the checklist (check [list] ++;) the output of the program should look like this:
7=(14,21,28,35,42,49,56)
14=(21,28,35,42,49,56,63)
21=(28,35,42,49,56,63,70)
28=(35,42,49,56,63,70,77)
35=(42,49,56,63,70,77,84)
42=(49,56,63,70,77,84,91)
49=(56,63,70,77,84,91,98)
14=1
21=2
28=3
35=4
42=5
49=6
56=7*
63=6
70=5
77=4
84=3
91=2
98=1
And the Final Step, put the result that appeared more often, highlight and print the number:
Resultado Vencedor = 56*