Liste des Groupes | Revenir à cl c |
edit: my attempt passed 9 of 14 tests, but fails on large N because
it's not calculating sum(1..N) correctly. Line 10. See anything?
>
>
//identify the missing number in a set of otherwise consecutive integers
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int i = 0, N = 0, temp = 0;
int64_t totN = 0, totInputs = 0;
scanf("%d", &N); //number of elements
for (i = 0; i < N-1; i++) { //list of elements
scanf("%d", &temp); //to temp var
totInputs += temp; //running total
}
totN = (N * (N + 1)) / 2; //sum of numbers 1 to N
printf("N %lld\n",N);
printf("tot N %lld\n",totN);
printf("tot inputs %lld\n",totInputs);
printf("%d\n", totN - totInputs); //solution
return 0;
}
>
>
sample output
N 50000
tot N -897458648 FAIL (should be 1250025000)
tot inputs 1250017374 GOOD
-2147476022 FAIL (should be 7626)
Les messages affichés proviennent d'usenet.