Sujet : Re: Suggested method for returning a string from a C program?
De : nospam (at) *nospam* dfs.com (DFS)
Groupes : comp.lang.cDate : 19. Mar 2025, 05:42:57
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vrdi0g$47cb$3@dont-email.me>
References : 1 2
User-Agent : Betterbird (Windows)
On 3/18/2025 11:26 PM, Keith Thompson wrote:
DFS <nospam@dfs.com> writes:
There's your problem.
https://cses.fi/problemset/text/2433
"In all problems you should read input from standard input and write
output to standard output."
ha! It usually helps to read the instructions first.
The autotester expects your program to read arguments from stdin, not
from command line arguments.
It probably passes no arguments to your program, so argv[1] is a null
pointer. It's likely your program compiles (assuming the NBSP
characters were added during posting) and crashes at runtime, producing
no output.
I KNEW clc would come through!
Pretty easy fixes:
1 use scanf()
2 update int to long
3 handle special case of n = 1
4 instead of collecting the results in a char variable, I print
them as they're calculated
The algorithm part was very simple and correct. Later ones won't be so easy. I coded 4 so far (but just submitted this one here), and plan on doing all 300.
https://imgur.com/bq0pKIwDid you hear a boom?
Thanks again!
updated code that passes:
===============================================================
// If n is even, divide it by two.
// If n is odd, multiply it by three and add one.
// Repeat until n is one.
// example: the sequence for n=3 is 3 10 5 16 8 4 2 1
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
long n = 0;
scanf("%ld", &n);
printf("%ld ",n);
while(1) {
if(n==1) {exit(0);}
if((n % 2) == 0)
{n /= 2;}
else
{n = (n * 3) + 1;}
if(n != 1)
{printf("%ld ",n);}
else
break;
}
printf("1\n");
return 0;
}
===============================================================