简体   繁体   English

代码在代码块上运行良好,但无法在SPOJ上运行

[英]The code runs fine on codeblock but fails to run on SPOJ

This was my first question on spoj,”test - Life, the Universe, and Everything” and i am highly demotivated towards competitve programming. 这是我对spoj的第一个问题,“测试-生命,宇宙和一切”,并且我对参加竞争性编程深感兴趣。 This was my code and the link to the question is this 这是我的代码,问题的链接是这个

#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. 它在代码块上运行良好,但在spoj上却给出了错误。 Please someone help me. 请有人帮我。

As mentioned by the user he is solving some spoj problem just for the benefit of other user below is the requirement 正如用户所提到的,他正在解决一些问题,只是为了其他用户的利益,下面是要求

Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. 您的程序将使用蛮力方法来找到生命,宇宙和一切的答案。 More precisely... rewrite small numbers from input to output. 更准确地说...将小数字从输入重写为输出。 Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits. 读完数字42后,停止处理输入。输入中的所有数字都是一位或两位数字的整数。

Example

Input:
1
2
88
42
99
Output:
1
2
88

Below is the code used (C++14) code worked in your online judge 以下是在线法官中使用的代码(C ++ 14)

Easiest way is just do this (working in you online judge) Keep taking input until you see 42 and then break 最简单的方法就是这样做(在您的在线裁判中工作)继续输入直到看到42,然后中断

as suggested one of the user WhozCraig (please see comment) 如用户之一WhozCraig的建议(请参阅评论)

int main()
{
    int n;
    while(std::cin >> n && n != 42)
        std::cout << n << '\n';
}

This is my code hope it helps: 这是我的代码希望对您有帮助:

int n;
cin>>n;
while (!(n==42))  {
    cout<<n<<"\n";
    cin>>n;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM