Sum Problem
Time Limit : 1000/500ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 24 Accepted Submission(s) : 15
Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
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>
#include<cmath>
#include<iomanip>
using namespace std;
int main(void)
{
double n;
double sum = 0;
while (cin >> n)
{
sum = 0;
for (double i = 1; i <= n; i++)
{
sum += i;
}
cout << fixed << setprecision(0) << sum << endl << endl;
}
return 0;
}
本文详细解析了HDOJ平台上的经典求和问题,通过示例输入输出,介绍了如何使用C++实现求和算法。该算法适用于计算从1到n的整数之和,且结果在32位带符号整数范围内。
2439

被折叠的 条评论
为什么被折叠?



