c++编程,注意,用c++(cin,cout)
c++编程,注意,用c++(cin,cout)DescriptionInthisproblem,yourtaskistocalculateSUM(n)=1+2+3+......
c++编程,注意,用c++(cin,cout)Description
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. You can assume the result will be in the range of 32-bit signed integer.
Process to the end of file.
Sample Input
1
100
Sample Output
1
5050 展开
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. You can assume the result will be in the range of 32-bit signed integer.
Process to the end of file.
Sample Input
1
100
Sample Output
1
5050 展开
展开全部
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
int sum(int max)
{
int result = 0;
for(int i = 1; i <= max; ++i)
result += i;
return result;
}
int main()
{
string line;
while(getline(cin,line))
{
int num;
stringstream ss(line);
ss >> num;
cout << sum(num) << endl;
}
return 0;
}
====================================
./a.out << EOF
> 1
> 100
> EOF
================================
1
5050
#include <iostream>
#include <sstream>
using namespace std;
int sum(int max)
{
int result = 0;
for(int i = 1; i <= max; ++i)
result += i;
return result;
}
int main()
{
string line;
while(getline(cin,line))
{
int num;
stringstream ss(line);
ss >> num;
cout << sum(num) << endl;
}
return 0;
}
====================================
./a.out << EOF
> 1
> 100
> EOF
================================
1
5050
更多追问追答
追问
哥,不用这么复杂吧?
问一下输出的5050上边的那个1是怎么来的?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询