杭电1001(人生路自己走)

 

Problem 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, 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
 

 

问题描述

在这个问题,你的任务是计算总和(n)= 1 + 2 + 3 +…+ n。

输入

输入将包含一系列整数n,每行一个整数。

输出

对于每个案例,输出数目(n)在一行,其次是一个空行。你可能会认为结果将是在32位带符号整数。

样例输入

1 100

样例输出

1 5050

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

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

A了还长时间,人生路 自己走

posted on 2015-04-20 15:15  Randy77  阅读(133)  评论(0编辑  收藏  举报

导航