61 / 92
Jan 2015

That code is not comparing the value of x to zero. It is comparing the result from the scanf function to zero.

scanf("%d",&x) > 0

Scanf returns the number of items that it read or an error of some sort.

1 month later

Here is my solution which is "accepted", 0.00 time and 2.00M used, in C language.

#include<stdio.h>
int main(void) {
    int x;
    x=0;
    while(x!=42) {
        scanf("%d",&x);
        if(x!=42)
            printf("%d\n",x);
    }
    return 0;
}

And here is a for-fun-code which solve the same problem, but closer to the example was given in the problem page. In C language too. Use in/output file.

#include<stdio.h>
#include<stdlib.h>
#define fi "d:\\program\\text\\ipTEST.txt"
#define fo "d:\\program\\text\\opTEST.txt"
struct Node {
  int data;
  struct Node *next;
};
struct Node *head;
void Inl(int); //insert last
void Clr(void); //clear
void Input(void);
void Output(void);
int main(void) {
  head=NULL;
  Input();
  Output();
  return 0;
}
void Inl(int x) {
  struct Node *t;
  t=(struct Node *)malloc(sizeof(*t));
  t->data=x;
  t->next=NULL;
  if(head==NULL)
    head=t;
  else {
    struct Node *u;
    u=head;
    while(u->next!=NULL)
      u=u->next;
    u->next=t;
  }
}
void Clr(void) {
  struct Node *prev,*curr;
  prev=head;
  head=NULL;
  curr=prev->next;
  free(prev);
  while(curr!=NULL) {
    prev=curr;
    curr=curr->next;
    free(prev);
  }
}
void Input(void) {
  FILE *f=fopen(fi,"r");
  if(!f)
    return;
  else {
    int x;
    while(!feof(f)) {
      fscanf(f,"%d",&x);
      Inl(x);
    }
    fclose(f);
  }
}
void Output(void) {
  FILE *f=fopen(fo,"w");
  if(!f)
    return;
  else {
    struct Node *t;
    t=head;
    while(t->data!=42) {
      fprintf(f,"%d\n",t->data);
      t=t->next;
    }
    Clr();
    fclose(f);
  }
}
8 days later

You can find many tutorial about solution test in c language in w3school and youtube.

27 days later

#include <stdio.h>
int main(void) {
	        int a;
		while(1){
		scanf("%d",&a);
		if(a== 42)
		break;
		else
		printf("%d\n",a);
		}
    return 0;
}
8 months later
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
   int size;
   int count=0;
   int n,i;
   int *str = (int *) malloc(sizeof(int));
   size=sizeof(int);
       
   n=0;
   do
   {
               scanf("%d",&n);
               if(count==0)
               {
                           str[0]=n;
                           ++count;
               }
               else
               {
                   size+=sizeof(int);
                   str = (int *) realloc(str, size);
                   str[count++]=n;
                   
               }
   }while(n<99);
   
   
   for(i=0;i<count;i++)
   {
                       if(str[i]==42)
                                     break;
                       else
                           printf("%d\n",str[i]);
   }

   free(str);
   return(0);
}

Hello guys, I'm new at spoj. I solved this TEST problem. It's working fine in my pc by spoj is telling me - "wrong answer". I cannot figure out what's wrong in the code. So, please help. Thanks in advance.

7 months later

i have run this program on turboc++ and it gives correct result the why spoj shows wrong answer?

include

int main(void)
{
int a[20],n,i;
printf("enter the total numbers you want to enter\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]==42)
break;
printf("%d\n",a[i]);
}
return 0;
}

Two reasons:
1. You expect to get the number of inputs at the start of your program. That is correct for lots of SPOJ problems, but not here. Your program has to read an unknown number of inputs until it finds '42'.
2. Everything you print will be take as an output of your program. So the judge "judges" the line "enter the total numbers you want to enter", and that is not an expected result.

2 months later

include

int main(void) {
int a[50],i,temp=0,n;

printf("Enter the elements");

for(i=1;i>0;i++)
{	
	scanf("%d",a[i]);
	if(a[i]==42)
	{	temp=i;
		break;
	}
}

for(i=1;i<temp;i++)
{ printf("%d",a[i]);
}
return 0;
}

my code is this still it gives RUNTIME ERROR?? WHY?

  • List item

[quote="abhishek22, post:68, topic:47"]

scanf("%d",a[i]);

[/quote]

Probably this should read scanf("%d",&a[i]); ?
Did you read the post directly above of yours?:confused:

include

int main()
{
int n, mq[1001], ms[1001], current, i, j=0;

scanf("%d", &n);

while (n>0)
{
	for (i = 0; i<n; i++)
	scanf("%d",&mq[i]);
	current = 1;

	for (i = 0; i<n; i++){
		while(j>0 && ms[j]==current){
			current++;
			j--;
		}
		if (mq[i]==current){
			current++;
		}else{
			j++;
			ms[j] = mq[i];
		}
	}
	while(j>0 && ms[j]==current){
		current++;
		j--;
	}
	if (current == n+1){
		printf("%s","yes");
	}else{
		printf("%s","no");
	}

	scanf("%d", &n);
}
return 0;

}
why I receive RUN TIME ERROR? Please T_T

2 months later

we use for loop only for repeating when we khow the limit how times it will repeat. and for condition we use test conditions such as if ,if else or switch cases

1 month later

This solution no make sense, the solution don't does what it need do.

The code from Noix do:

input
output
input
output
... until you input 42

The problem ask you do

input
input
....until you input 42

output
output
...when 42 stop output.

someone for the love of GOD can explain me what you want we do in this problem ?

it is correct and efficient to do it the way like noix did. (though other approaches can get accepted, too)

  • read a value from input stream
  • print that value to output stream
23 days later

include

using namespace std;

int main(){
int arr[10],i, m = 42;
for (i = 0; i < 10;i++){
cin >> arr[i];

	if (arr[i] == m){
		break;
	}
	cout << arr[i] << " ";
}



return 0;
}
2 months later

Hi, this is the code I put for resolution for this problem, but every time SPOJ says Time Limit Exceeded:

#include
int main(int i)
{
while(scanf("%d",&i)>0 && printf("%d\n",i) && i != 42 );
return 0;
}

Could you please help me on this ?

3 months later

This was my first question on spoj and i am highly demotivated towards competitve programming.
this was my code

#include <iostream>
using namespace std;
int main() {
int a[10],i;
 for(i=0;i<10;i++)
 { cin>>a[i];
 }
for(i=0;i<10;i++)
 {
  if(a[i]!=42)
   cout<<a[i]<<endl;
   else
   break;
 }
	return 0;
}

It is running fine on codeblocks but is giving errors here on spoj.
Please someone help me.

1 month later

I wonder why it tell me time limit exceed??

#include <stdio.h>
int main()
{
int i=0,number[10];
do
{
scanf("%d",&number[i]);
i++;
}
while(number[i-1]!=42);

for(int j=0;j<i-1;j++)
{
    printf("%d\n",number[j]);
}

return 0;

}

5 months later

I tried making as short as possible for fun and I had this:

for (short x = 0; x != 42; scanf("%hd", &x), (x != 42)&& printf("%hd\n", x));
return 0;

But initially I had this:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>

#define ERROR_RET_NULL(x) fprintf(stderr, "ERROR: %s returned NULL in file '%s' at line: %i.", x, __FILE__, (__LINE__ - 2))


int 
main(void) 
{
        while (true)
        {
        	short temp = 0;

                char userInputString[5];
                char *endPtr;
                
                if (fgets(userInputString, 5, stdin) == NULL)
                {
                        ERROR_RET_NULL("fgets");
                        exit(EXIT_FAILURE);
                }
                else
                {
                        char *checkPtr = strchr(userInputString, '\n');
                        if (checkPtr) *checkPtr = '\0';
                        
                        long numberToCastToShort = strtol(userInputString, &endPtr, 10);
                        if (numberToCastToShort > SHRT_MAX || numberToCastToShort < SHRT_MIN)
                        {
                                /* Given the size restriction on the string this is basically deadcode */
                                fprintf(stderr, "ERROR: number too big");
                                exit(EXIT_FAILURE);
                        }
                        else if (numberToCastToShort == 42) break;
                        else temp = (short) numberToCastToShort;
                        
                }
                printf("%d\n", temp);
        }
        
        return 0;
}
2 months later

I am not getting the output required… It ask for the number but then page closes after entering the number.