Sujet : Re: Suggested method for returning a string from a C program?
De : nospam (at) *nospam* dfs.com (DFS)
Groupes : comp.lang.cDate : 20. Mar 2025, 03:34:04
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vrfuqs$2608a$2@dont-email.me>
References : 1 2 3 4 5
User-Agent : Betterbird (Windows)
On 3/19/2025 5:56 PM, Keith Thompson wrote:
DFS <nospam@dfs.com> writes:
On 3/19/2025 4:53 AM, Keith Thompson wrote:
I used a different approach. I'll encode the description of the
solution using rot13.
The program is given an integer n and a list of n-1 integers, not
necessarily ordered.
The task is to determine which number is missing from the list.
Gurer'f n jryy xabja sbezhyn sbe gur fhz bs nyy a ahzoref sebz bar
gb a. Pbzchgr gur rkcrpgrq fhz, gura fhogenpg gur fhz bs gur ahzoref
tvira ba gur frpbaq vachg yvar. Gur qvssrerapr vf gur zvffvat ahzore.
That's dead simple! It works because the input numbers start with 1.
I'll give it a quick try.
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)