码迷,mamicode.com
首页 > 其他好文 > 详细

ACM HDU 1001

时间:2017-09-20 23:28:05      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:sig   inpu   class   提交   output   putchar   follow   scanf   ott   

 

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.
 

Sum Problem

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
 
解题思路:很简单,求累加和。然而我提交了9次才AC了两次。
代码如下
第一次AC
#include <stdio.h>

int sso(int n);
int main(void)
{
    int i;
    while (scanf_s("%d", &i) != EOF)
    {
        printf("%d\n",sso(i));
        putchar(‘\n‘);
    }
        return 0;
}
int sso(int n)
{
    int i, sum=0;
    for (i = 0; i <= n; i++)
        sum += i;
    
    return sum;
}
第二次AC
#include <stdio.h>
int main(void)
{
    int a = 0, b, sum = 0;
    while (scanf_s("%d", &b) != EOF)
    {
        sum = 0, a = 0;
        do {
            a = a + 1;
            sum = a + sum;
        } while (a<b);
        printf("%d\n", sum);
        printf("\n");
    }
    return 0;
}
 

ACM HDU 1001

标签:sig   inpu   class   提交   output   putchar   follow   scanf   ott   

原文地址:http://www.cnblogs.com/WLHBlog/p/7565262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!