杭电oj1001

*题目

       In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.

       Input

        The input will consist of a series of integers n, one integer per line.

       Output

       For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit     signed integer.

       Sample Input

 

1 100

 

 

Sample Output

 

1 5050

 

*解答

错解

其实错误还有好多个版本。。。比如弄错了输入输出的格式,以为要输入输出两个数字等等等等

#include <iostream>
using namespace std;
int main()
{

   int b;
    int sum=0;
    for(int i=1;i<=b;i++)
    {
       sum=sum+i;
    }
       cout<<sum<<endl;
    
return 0;
}

正解

#include <iostream>
using namespace std;
int main()
{

   int b;

   while(cin>>b)
   {
       int sum=0;
    for(int i=1;i<=b;i++)
    {
       sum=sum+i;
    }
       cout<<sum<<endl<<endl;
    }
return 0;
}

   做这题的时候发现刚做完1000题,1000题下的关联题目就有1001题,但是做的时候还是傻了,没有以输入多组数据的方式去做,但是更为诡异的事,即使这么做了之后,依旧是不断的PE,最后百度发现不仅要换行还要加一个空行

难受。。。。

猜你喜欢

转载自blog.csdn.net/qq_42770849/article/details/81172379