Regarding the example with 3 4 6 0 2 9 it is clear that 6 0 2 add up to 8 and it is ok, the sum of all numbers gives 24 so this is the second solution.
A third solution is 0 by itself, as would be the case for any multiple of 8 in a square. (@Kiritonito pointed this out to me)
A general notation
A possible solution can be written as a pair (i,j)
with the start in i
and the length j
of the sequence.
To 8 0 8 0 8
we have
0, 1 0, 2 0, 3 0, 4 0, 5
1, 1 1, 2 1, 3 1, 4 2, 1
2, 2 2, 3 3, 1 3, 2 4, 1
As 15
solutions.
Particular case
- With all squares with multiples of 8 the total solutions should be
(N+1)*N / 2
, and 10 squares with 8 should lead to 55 solutions
An example in C
I changed a little the original program to record on file Q8.txt the solutions found, so you can check the program and see the "squares"found.
#define LIMITE_ (1000 * 1000)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
const char* s_padrao = "q8.txt"; // entrada padrao
unsigned limite = LIMITE_; // limite padrao
char arquivo_s[80];
char na_tela = 0;
char linha[256];
if (argc > 1)
strcpy(arquivo_s, argv[1]);
else
strcpy(arquivo_s, s_padrao);
//fprintf(stderr, "Saida em \"%s\"\n", arquivo_s);
FILE* S = fopen(arquivo_s, "a");
if (S == 0) return -1; // nao abriu saida
unsigned N;
int res = scanf("%d", &N);
fgets(linha, 256, stdin); // le o resto da linha
if (res != 1) return -2;
if (N > LIMITE_) N = LIMITE_;
fprintf(S,"%d numeros\n", N);
short* V = (short*)malloc(sizeof(short) * N);
if (V == NULL) return -3;
// le os numeros ate preencher o vetor inicial
for (unsigned i = 0; i < N; i += 1)
{
res = scanf("%hd", (V+i) );
if (res != 1)
return -4;
else
fprintf(S, "%6d", *(V + i));
}
fprintf(S, "\n");
// busca as solucoes
unsigned total = 0;
for (unsigned i = 0; i < N; i += 1)
{
if (*(V+i) % 8 == 0)
{
fprintf(S, "%d, 1\n", i);
total += 1;
}
unsigned soma = *(V+i); // o primeiro, claro
for (unsigned j = i+1; j < N; j += 1)
{
soma += *(V + j);
if ((soma % 8) == 0)
{
fprintf(S, "%d, %d\n", i, 1+j-i);
total += 1;
};
}
}
fprintf(S, "Total: %d solucoes\n\n", total);
fclose(S);
printf("%d\n", total);
free(V);
return 0;
}; // main()
Exit in Q8.txt (examples)
This is the generated file
6 numeros
3 4 6 0 2 9
0, 6
2, 3
3, 1
Total: 3 solucoes
7 numeros
1 1 1 1 1 1 1
Total: 0 solucoes
5 numeros
8 0 8 0 8
0, 1
0, 2
0, 3
0, 4
0, 5
1, 1
1, 2
1, 3
1, 4
2, 1
2, 2
2, 3
3, 1
3, 2
4, 1
Total: 15 solucoes
4 numeros
0 0 0 0
0, 1
0, 2
0, 3
0, 4
1, 1
1, 2
1, 3
2, 1
2, 2
3, 1
Total: 10 solucoes
4 numeros
8 16 24 32
0, 1
0, 2
0, 3
0, 4
1, 1
1, 2
1, 3
2, 1
2, 2
3, 1
Total: 10 solucoes
exit at the terminal
6
3 4 6 0 2 9
3
7
1 1 1 1
1 1 1
0
5
8 0 8 0 8
15
Back to the original program
if (n <= 0 || n > 1000000) printf("valor invalido!\n\n informe outro:\n");
} while (n < 0 || n > 1000000);
int numeros[n];
for (i = 0; i < n; i++) {
do {
printf("informe os numeros(de 1 a 1000): ");
scanf("%d", & numeros[i]);
if ((numeros[i] < 0 || numeros[i] > 1000)) printf("valor invalido!\n\n informe outro:\n");
} while (numeros[i] < 0 || numeros[i] > 1000);`
Suggestions
- declare control variables of
for
in charge itself
- when using
do/while
avoid constructions like this where you test the same thing twice
main()
returns int.
- declare constant dimension vectors or use malloc() and create the vector on time.
Read https://pt.meta.stackoverflow.com/questions/8388/que-erro-cometi-ao-formular-minha-pergunta?cb=1 and see if there’s anything you can do to improve your question.
– Clarck Maciel
Hello Vitor, already debug line by line to identify the problem? The debug is part of learning and helps to better understand how the program works, I suggest doing this, there will probably solve or reach a point that can not, and it will be easier to ask for help for example "gave error or invalid value in line XXX in the YYY command" :)
– Ricardo Pontual