2020-12-15 19:25:03 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-01-04 15:06:57 +01:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#define NBVAL 25 /* Le nb de val à prendre, mit sur 5 pour les test */
|
|
|
|
int prem; /* la réponse à la partie 1 */
|
2020-12-15 19:25:03 +01:00
|
|
|
|
|
|
|
void prem_partie(FILE *ptr)
|
|
|
|
{
|
|
|
|
int val, find = 0;
|
|
|
|
int i = 0, j, k;
|
|
|
|
int first = 0; /* permet de check si on a bien fait les n premiers */
|
|
|
|
int prev_val[NBVAL]; /* tableau contenant les n valeurs précédentes */
|
|
|
|
fscanf(ptr, "%d", &val);
|
|
|
|
|
|
|
|
while (!feof(ptr) && !find)
|
|
|
|
{
|
|
|
|
if (first < NBVAL)
|
|
|
|
first++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
while (j < NBVAL && !find)
|
|
|
|
{
|
|
|
|
k = 0;
|
|
|
|
while (k < NBVAL && !find)
|
|
|
|
{
|
|
|
|
if (k != j && !find)
|
|
|
|
find = prev_val[j] + prev_val[k] == val ? 1 : 0;
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
find = find ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = (i+1) % NBVAL;
|
|
|
|
if (!find)
|
|
|
|
{
|
|
|
|
prev_val[i] = val;
|
|
|
|
fscanf(ptr, "%d", &val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 15:06:57 +01:00
|
|
|
prem = val;
|
|
|
|
printf("La première valeur ne correspondant pas est %d.\n", val);
|
2020-12-15 19:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void deux_partie(FILE *ptr)
|
|
|
|
{
|
2021-01-04 15:06:57 +01:00
|
|
|
int *tab = NULL;
|
2021-01-04 15:17:01 +01:00
|
|
|
int size = 2, born_inf = 0;
|
|
|
|
int find = 0;
|
2021-01-04 15:06:57 +01:00
|
|
|
while (!feof(ptr) && !find)
|
|
|
|
{
|
|
|
|
tab = (int *) malloc(sizeof(int) * size);
|
|
|
|
|
|
|
|
while (!find && !feof(ptr))
|
|
|
|
{
|
|
|
|
go_line(ptr, born_inf);
|
|
|
|
for (int i = 0; i < size; i++)
|
2021-01-04 15:17:01 +01:00
|
|
|
fscanf(ptr, "%d", (tab + i));
|
2021-01-04 15:06:57 +01:00
|
|
|
|
|
|
|
if (sum_ptr(tab, size) == prem)
|
|
|
|
find = 1;
|
2021-01-04 15:17:01 +01:00
|
|
|
|
2021-01-04 15:06:57 +01:00
|
|
|
born_inf++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!find)
|
|
|
|
{
|
|
|
|
born_inf = 0;
|
|
|
|
size++;
|
|
|
|
free(tab);
|
|
|
|
fseek(ptr, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int inf = *tab, sup = *tab;
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
if (inf > *(tab+i))
|
|
|
|
inf = *(tab+i);
|
|
|
|
if (sup < *(tab+i))
|
|
|
|
sup = *(tab+i);
|
|
|
|
}
|
|
|
|
|
2021-01-04 15:17:01 +01:00
|
|
|
free(tab);
|
2021-01-04 15:06:57 +01:00
|
|
|
int last = sup + inf;
|
2020-12-15 19:25:03 +01:00
|
|
|
|
2021-01-04 15:17:01 +01:00
|
|
|
printf("La somme des nombres faisant %d est %d.\n", prem, last);
|
2020-12-15 19:25:03 +01:00
|
|
|
}
|