`
richard_ma
  • 浏览: 15155 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

hdu1001 求和

阅读更多
Sum Problem

http://acm.hdu.edu.cn/showproblem.php?pid=1001

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 <stdio.h>
#include <stdlib.h>

int main (int argc, char const* argv[])
{
    int e, ans, i;

    while (scanf("%d", &e) != EOF) {
        ans = 0;
        for (i = 1; i <= e; i++) {
            ans += i;
        }
        printf("%d\n", ans);
        printf("\n");
    }

    return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics