HDOJ | 1001 Sum Problem

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

Sum Problem

Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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
 
 

Code in C

#include <stdio.h>

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

反思

while(scanf("%d", &n)!=EOF)

于我来说,做题的时候没有想到这行代码

解释一下:不断地读取n,直到文件末尾

猜想:这网站测试代码用的测试用例是保存在文件中的?

发布了38 篇原创文章 · 获赞 5 · 访问量 6560

猜你喜欢

转载自blog.csdn.net/shaotianyang12/article/details/103439212
今日推荐