杭电1001

来源:互联网 发布:wow3.35数据库 编辑:程序博客网 时间:2024/05/17 01:42

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

/*******************项目名称:杭电OJ1002创建时间:2017/6/14********************/#include<stdio.h>int main(){    int i, n;    int sum = 0;    while (scanf("%d", &n) != EOF)    {        for (i = 1; i <= n; i++)            sum += i;        printf("%d\n\n", sum);//输出结果后在输出一行        sum = 0;              //将sum重新置位0,方便观察    }    return 0;}

tips:从1加到n,循环因该是小于等于n,每次生成成功后都要把sum的值重新置为0,以免影响后面的实验。

注意:每输出一个和之后要空一行,所以是两个“\n”。

原创粉丝点击