1153: sum

1153: sum

时间限制: 1 Sec   内存限制: 128 MB
提交: 70   解决: 26
[ 提交][ 状态][ 讨论版]

题目描述

Hey, welcome toPDSU OJPingdingshan University Online Judge). In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.

输入

The input will consist of a series of integers n, and n>=0,end of file EOF,one integer per line.

输出

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. 

样例输入

1
100

样例输出

1

5050

翻译:

题目描述:
       嘿,欢迎topdsu ojpingdingshan大学在线判断)。在这个问题中,您的任务是计算和(n)=1 2 3…n.
输入:
      输入一系列的整数n,n>=0,文件结束EOF,每行一个整数。
输出:
     对于每一种情况下输出,输出和(N)在一条线上,用一个空行。您可以假设结果将在32位有符号整数的范围内。
代码:
#include<stdio.h>
int main()
{
    int n,i,sum=0;  //n为有几组数据,sum为和
    while(scanf("%d",&n)!=EOF)  //多组数据测试
    {
        sum=0;  //定义变量sum的初始值为0
        for(i=1;i<=n;i++)  //当i=1,i<=n时
        {
            sum+=i;  //累加,否则一直循环
        }
        printf("%d\n\n",sum);  //输出和,\n\n为一个空行
    }
    return 0;
}
【注】输出第一个为1到1的和,第二个为1到100的和
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值