🔻PS/Baekjoon
[Baekjoon] 백준 2355 시그마 C++
_니지
2022. 11. 10. 11:55
https://www.acmicpc.net/problem/2355
2355번: 시그마
첫째 줄에 두 정수 A, B가 주어진다. (-2,147,483,648 ≤ A, B ≤ 2,147,483,647)
www.acmicpc.net
문제
두 정수 A와 B가 주어졌을 때, 두 정수 사이에 있는 수의 합을 구하는 프로그램을 작성하시오. 사이에 있는 수들은 A와 B도 포함한다.
입력
첫째 줄에 두 정수 A, B가 주어진다. (-2,147,483,648 ≤ A, B ≤ 2,147,483,647)
출력
첫째 줄에 답을 출력한다. (-2,147,483,648 ≤ 답 ≤ 2,147,483,647)
예제 입력 1
1 2
예제 출력 1
3
코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <functional>
#include <cmath>
#include <map>
#include <set>
#define _CRT_SECURE_NO_WARNINGS
#define INF 987654321
#define ll long long
typedef pair<int, int> PII;
int main() {
ll a, b, ans;
cin >> a >> b;
if (a < b) {
ans = b * (b + 1) / 2 - a * (a - 1) / 2;
}
else {
ans = a * (a + 1) / 2 - b * (b - 1) / 2;
}
cout << ans << "\n";
return 0;
}
|
cs |
728x90
반응형