Sum Problem
做題地址:http://acm.hdu.edu.cn/showproblem.php?pid=1001
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
這一題,額,,,,
公式:
1 + 2 + 3 + … + n = n * (n - 1) / 2
下面是程式碼
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main() {
long long n;
while (~scanf("%lld", &n)) {
printf("%lld\n\n", n * (n + 1) / 2);
}
return 0;
}
相關文章
- Mathematical Problem
- Yet Another Problem
- Nanami and the Constructive ProblemNaNStruct
- Prime Ring Problem
- 2019 MCM Problem A
- 秒殺 2Sum 3Sum 4Sum 演算法題演算法
- GCD SUMGC
- 集合sum
- 15+18、3Sum 4Sum
- E. Not a Nim Problem
- Nanami and the House Protecting ProblemNaN
- HDU - 6182 A Math Problem
- Problem A. Ascending Rating
- Fixed "There was a problem with the editor 'vi'"
- Prime Ring Problem (dfs)
- Range Minimum Sum
- B - Minimum Sum
- Missing Subsequence Sum
- Path Sum III
- Leetcode Path SumLeetCode
- leetcode Sum系列LeetCode
- Sum of Left Leaves
- Path-sum
- P1865 A % B Problem
- Assignment Problem的若干思考
- A + B Problem II hd 1002
- Joe Harris is a real problem with this team
- Follow/Unfollow problem in system design
- Euclid Problem - PC110703
- HDU 1002 A + B Problem II
- POJ 2355 Railway Ticket problemAI
- The Door Problem 並查集並查集
- HDU 1792 A New Change Problem
- LeetCode | 1 Two SumLeetCode
- SQL groupby sum 用法SQL
- 7.22 APPROX_SUMAPP
- the Sum of Cube hd 5053
- Leetcode 39 Combination SumLeetCode