1001 Sum Problem

Sum Problem

Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 156066    Accepted Submission(s): 36596


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
 
今天开始做杭电的ACM,觉得挺有趣的,
刚好可以来练练编程能力。
好久没有好好编了,
写的代码看起来都觉得很傻瓜,
没办法,慢慢开始吧
#include <stdio.h>
int sum(int);

int main(){
    int input,output;
    while (scanf("%d",&input)==1)
    {
        output = sum(input);
        printf("%d\n\n",output);
    }
    return 0;
}
int sum(int n){
    int sum,i;
    sum=0;
    for (i=1;i<=n;i++)
    {
        sum+=i;
    }
    return sum;
}

 

 网络代码:
#include <stdio.h>
int main(){
    int n, sum;
    while (scanf("%d", &n) == 1){
        sum = 0;
        while (n != 0){
            sum += n--;
        }
        printf("%d\n\n", sum);
    }
    return 0;
}

 

 
 
 
 
 
posted @ 2012-05-17 00:19  zwein  Views(966)  Comments(0)    收藏  举报