您的位置:首页 > 编程语言 > C语言/C++

HDU 1001 Sum Problem C/C++

2015-02-07 10:27 453 查看
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

Author

DOOM III

C语言:

#include <stdio.h>
main() {
int sum, n, i;
while (scanf("%d", &n) != EOF) {
sum = 0;
for (i = 0; i <= n; i++) {
sum += i;
}
printf("%d\n\n", sum);
}
}


C++:

#include <iostream>
using namespace std;
main() {
int sum, n, i;
while (cin >> n) {
sum = 0;
for (i = 0; i <= n; i++) {
sum += i;
}
cout << sum << endl << endl;
}
}


在这里提醒大家,换行符(\n和endl)一定要两个,否则会提示Presentation Error.

代码仅供参考,如果哪里有不足,欢迎各位指教,我一定会及时进行改进和优化。

如果要转载,请注明出处。

2015.2.7 黑骐
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: