54 / 92
Nov 2014

Can i know the output for the following inputs(asking only for my clarity...don't mind.... confused )

1.) 1 12 453 32 45 67 89 987 42
2.) 123 123 123 42
3.)98 56 45 34 23 19 12 67 89 123 454 87 90 1 42

Thanks in Advance

None of those test cases are valid. The judge will not provide invalid test cases.

27 days later

i wrote the following code
and in my terminal im getting the desired output but for some reason in the SPOJ its showing wrong answer
please help !! im net to SPOJ;

#include<stdio.h>
#include<stdlib.h>
int main()
{
  int a;
  scanf("%d",&a);
  while(a>0&&a!=42)
  {
    printf("%d",a);
    scanf("%d",&a);
  }
  return 0;
}

Your code does not return the correct output for the sample input.

For this input:

1
2
88
42
99

Your code will print:

1288

The correct output is:

1
2
88

The difficulty that you are having is caused by the fact that you are using the console for input. The console allows you to type input to stdin as well as display output from stdout.

If you redirect your stdin and stdout to a file then you will accurately see what is being entered from stdin and being output to stdout.

To fix your output issue you should change your printf statement by adding a newline character to the end of it:

printf("%d\n",a);

Why have you chosen these conditions for your while loop?

while(a>0&&a!=42)

I got the same output as noix by implementing the following code but i get 'wrong answer' when i submit it on spoj please help me out
The Code:

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

The Code When ran in ideone gave same result!

http://ideone.com/jKSYau4
http://ideone.com/XyJUag9

1 month later

I just can't figure put what is the problem with my code -

#include<stdio.h>
int main()
{
	int i, k[5];
    i=0;

while(i != 5)
{
scanf("%d", &k[i]);
i++;

}

i=0;

while (i !=5 )
{
	printf("%d", k[i]);
	i++;
	
	if( k[i] < k[i-1])
	{break;}
	else{printf("\n");}

}


return 0;
}

Thank you.

1 month later

Why is my source code not accepted even though the expected output is obtained?

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

What's wrong with zero?

0
0
0
0
0
0
0
42
10
11
12

Thanks. I removed the x>0 condition and the code was accepted. But the program at the start of this thread is accepted even though it has x>0 condition. Why?

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

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