[HDU1001] Sum Problem

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

分析

输入n,求1+2+...+n的和。

方法有两种:

1. 直接求法

使用一个for循环进行累加。用s表示总和,s初始化为0,然后再维护一个循环变量i。代码:

int s = 0;
for (int i = 1; i <= n; i++)
    s += i;
printf("%d\n\n", s);

完整C程序:

#include <stdio.h>
int main()
{
    int n;
    while (scanf("%d", &n) != EOF)
    {
        int s = 0;
        for (int i = 1; i <= n; i++)
            s += i;
        printf("%d\n\n", s);
    }
    return 0;
}

2. 公式法

还记得高斯吧,他小时候就计算出了1+2+...+100=5050。方法1就像是其他同学,方法2则是高斯。

进入正题,等差数列有一个公式:总和=(首项+末项)*项数/2。这里首项=1,末项=n,项数=n,因此1+2+...+n=(1+n)*n/2。代码:

printf("%lld\n\n", (long long)(1 + n) * (long long)n / 2LL);

但有一个类型问题需要注意:description中说总和是不超过32位有符号整数的范围的(也就是2^31-1或2147483647),这说明(1+n)*n/2<=2147483647,但不代表(1+n)*n也是小于2147483647的。事实上,当n>=14654时,(1+n)*n就超过2147483647了。这种情况下,进行运算将会出现溢出错误。因此需要将1+n和n转换成long long(其实只要转1个就可以了,后面的2LL也可以直接写2)。当然,算出(1+n)*n后将其转成int再用%d打印也没有问题。(P.S. 我就是因为这个原因WA的……)

完整C程序:

#include <stdio.h>
int main()
{
    int n;
    while (scanf("%d", &n) != EOF)
        printf("%lld\n\n", (long long)n * (long long)(n + 1) / 2LL);
    return 0;
}

注意

每次输出要输出两个换行符!

时间: 2024-11-06 21:40:06

[HDU1001] Sum Problem的相关文章

Maxmum subsequence sum problem

We have a lot of ways to solve the maximum subsequence sum problem, but different ways take different time. 1.Brute-force algorithm int maxSubSum1(const vector<int> &a) { int maxSum=0; for(int i=0;i<a.size();i++) for(int j=i;j<a.size();j++

hdu 2576 Another Sum Problem

题目大意:求前n项和的前n项和. 数学推导题,f(n)=n*(n+1)*(n+2)/6 推导思路如下: #include"cstdio" #include"cstring" #include"cmath" #include"cstdlib" #include"iostream" #include"algorithm" #include"queue" using nam

NYOJ 927 The partial sum problem 【DFS】+【剪枝】

The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 One day,Tom's girlfriend give him an array A which contains N integers and asked him:Can you choose some integers from the N integers and the sum of them is equal to K. 输入 There are mul

nyoj927 The partial sum problem(dfs)

The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 One day,Tom's girlfriend give him an array A which contains N integers and asked him:Can you choose some integers from the N integers and the sum of them is equal to K. 输入 There are multi

NYOJ-927 The partial sum problem

The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 One day,Tom's girlfriend give him an array A which contains N integers and asked him:Can you choose some integers from the N integers and the sum of them is equal to K. 输入 There are multi

hdu2058 The sum problem(枚举~~等差数列求和公式)

The sum problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17697    Accepted Submission(s): 5275 Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possibl

The sum problem

The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16197 Accepted Submission(s): 4843 Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the possible sub

HDU 1001 Sum Problem

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. InputThe input will consist of a series of integers n, one integer per line. OutputFor each ca

HDU 1001 Sum Problem C/C++

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