Liste des Groupes | Revenir à cl c |
On 3/18/2025 8:38 PM, DFS wrote:The main issue, quickly spotted by Keith Thompson, was the program was supposed to read input from stdin. I had it reading from the command line (so no output), and using an int where long was needed (returned some wrong answers).>strdup.
I'm doing these algorithm problems at
https://cses.fi/problemset/list/
>
For instance: Weird Algorithm
https://cses.fi/problemset/task/1068
>
My code works fine locally (prints the correct solution to the console), but when I submit the .c file the auto-tester flags it with 'runtime error' and says the output is empty.
>
------------------------------------------------------------
// If n is even, divide it by two.
// If n is odd, multiply it by three and add one.
// Repeat until n is one.
// n = 3: output is 3 10 5 16 8 4 2 1
>
>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
>
int main(int argc, char *argv[])
{
int n = atoi(argv[1]);
int len = 0;
char result[10000] = "";
sprintf(result, "%d ", n);
>
while(1) {
if((n % 2) == 0)
{n /= 2;}
else
{n = (n * 3) + 1;}
>
if(n != 1)
{
len = strlen(result);
sprintf(result + len, "%d ", n);
}
else
break;
}
>
len = strlen(result);
sprintf(result + len, "1 ");
printf("%s\n",result);
>
return 0;
}
------------------------------------------------------------
>
Any ideas?
Thanks
https://www.geeksforgeeks.org/strdup-strdndup-functions-c/
Lynn
Les messages affichés proviennent d'usenet.