1 / 15
Feb 2016

Hello friends,

Im new to spoj and im trying to figure why my code is wrong, i searched the forum but nothing matched my problem.

It works on my compiler, but all i get from the online judge is wrong code.

Could you guys help me with this problem?

			#include <stdio.h>
			
			int main(){
				
				int input[5];
				int output[5];
				int i, j;
				
				for(i = 0; i < 5; i++){
					scanf("%i", &input[i]);
				}
				printf("\n");
				for(j = 0; j < 5;j++){
					
					if(input[j]==42){
						printf("%d\n", input[j]	);
						break;
					} else{
						printf("%d\n", input[j]);
					}
						
				}
				return 0;
			}
  • created

    Feb '16
  • last reply

    Apr '23
  • 14

    replies

  • 3.7k

    views

  • 12

    users

  • 4

    links

Just fixed an error, but the judge still says its wrong.

			#include <stdio.h>
			
			int main(){
				
				int input[5];
				int output[5];
				int i, j;
				
				for(i = 0; i < 5; i++){
					scanf("%i", &input[i]);
				}
				printf("\n");
				for(j = 0; j < 5;j++){
					
					if(input[j]==42){
						
						break;
					} else{
						printf("%d\n", input[j]);
					}
						
				}
				return 0;
			}

Problem says after you find 42 stop processing , it doesnt ask to break. Take another integer array then save elements before 42 in that another array , then sort the array as the problem says .

The input of this problem can have any length. So you should not use any array, just read and print line by line until you read 42.

Try to run Your code on this dataset:
1
2
3
4
5
6
7
42
8
9

The answer should be:
1
2
3
4
5
6
7

Finally, i had to change the hole logic but it got accepted. Are there any more sofisticated logic you guys would suggest?

#include <stdio.h>
//#include <stdlib.h>
		
		int main(){
			
			int output[100];
			int i = 0;
			int answer = 0;
            int count = 0;
            int count2 = 0;
			while (answer != 42){
                  
                  scanf("%i", &answer);
                  output[count] = answer;
                  count++;
            }
            
            while (count2 < count -1){
                  
                  printf("%d\n", output[count2]);
                  count2++;      
            }
            //system("PAUSE");
	    return 0;
		}
1 year later

What is wrong with this code? It works fine on my offline compiler but not over here. I'm new to SPOJ.

#include <iostream>
using namespace std;
int main() {
	
	int *p=new int;
	int i, n=5000, len=0;
	for(i=0; i<n; ++i){
		if(p!=NULL){
			cin>>(*(p+i));
			if(*(p+i)==42){
				break;
			}
		}
	}
	len=i;
	for(i=0; i<len; ++i){
		cout<<(*(p+i))<<'\n';
	}
	delete p;
	return 0;
}
2 months later

here is my logic. I was stuck with this problem coz i thought we had to sort the numbers
#include
#include
#include
using namespace std;

int main()
{
int x=0,i=0;
vector a;
while(x!=42)
{
cin>>x;
a.push_back(x);
}
a.pop_back();
//sort(a.begin(),a.end());
int l=a.size();
for(i=0;i<l;i++)
cout<<a[i]<<"\n";
}

Ccongratulations! :wink:

PS
Don’t put your AC code here, please :wink: Only if you have any problem and want a little help, advicess, tips. etc
BTW Your copy-paste code is awful :wink:
Compare:

#include
#include
#include
using namespace std;

int main()
{
   int x = 0, i = 0;
   vector a;
   while (x != 42)
   {
....
1 year later

hey there!
may u please tell me how can we post our source code to detect our errors(as I have tried some problems quiet a lot but still my answer is not getting accepted and I am not getting advice ),so this method will help me a lot to learn programming at optimum rate.

hello guys,i am new to spoj.can anyone tell what is the error in my code
import java.io.*;
class pro1
{
static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,i=1,k=0,a[]=new int[100];
while(i==1)
{
System.out.println(“enter number:”);
n=Integer.parseInt(br.readLine());
if(n==42)
break;
else
{a[k]=n;k++;}
}
for(i=0;i<k;i++)
System.out.println(a[i]);
}
}

You print messages like ‘enter number’. That will lead to wrong answer - you should only print what is asked in the statement.

(consider that the judge takes your entire output and compares it to the correct output. It can’t make sense of random messages you add in there)

2 years later

Hello can anyone tell me what is wrong with this python 3 code of mine
active = True
while active:
n = int(input('Enter the number= '))
if n == 42:
active = False
else:
print(n)
I am unable to find any error in this code of mine but it still shows wrong answer on SPOJ

1 year later

Even tho I don’t really understand where the “input” should come from in this exercise because it is not defined well…
Not well define because input can come from many places: command line parameters, local variables, console input, streams…

Here is my concise c++ solution assuming input come frome console input (write something into the console windows and press enter):

#include <iostream>
using namespace std;

int main()
{
  string n{};
  while (cin >> n)
  {
	if (n == "42")
	{
		return 0;
	}
	cout << n << endl;
  }
  return 0;
}