hdu 1001 解析

来源:互联网 发布:win7系统无法安装软件 编辑:程序博客网 时间:2024/06/14 17:48

Sum Problem

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


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
1100
 

Sample Output
15050
 

题意:多组测试数据,输入一个数n,计算1+2+……n。

思路:用for循环来解决。

注意点:每一组数据之间有一个空行。

代码如下:

#include<iostream>#include<cstdio>using namespace std;int main(){    int a,b;    while(cin>>a)    {        int sum=0;        for(int i=1;i<=a;i++)        sum+=i;        cout<<sum<<endl<<endl;    }    return 0;}




















0 0
原创粉丝点击