Little Jay

[C++] 백준 2018번 - 수들의 합 5 본문

알고리즘/BOJ

[C++] 백준 2018번 - 수들의 합 5

Jay, Lee 2022. 5. 23. 18:46

1, 2가 입력되었을때를 생각안했다가 틀렸던 문제

base case에 대해서도 항상 생각하자

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

int n;

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	cin >> n;

	int left = 1;
	int right = 2;
	int total = 1;

	int ans = 0;

	if (n == 1 || n == 2) {
		cout << 1 << endl;
		return 0;
	}

	while (left <= n) {
		total += right;

		if (total == n) {
			ans++;
			total = 0;
			left++;
			right = left;
		}
		else if (total > n) {
			left++;
			total = 0;
			right = left;
		}
		else right++;

		
	}

	cout << ans << endl;

	return 0;
}

'알고리즘 > BOJ' 카테고리의 다른 글

[C++] 백준 11404번 - 플로이드  (0) 2022.06.04
[C++] 백준 4358번 - 생태학  (0) 2022.06.02
[C++] 백준 12813번 - 이진수연산  (0) 2022.05.22
[C++] 백준 10282번 - 해킹  (0) 2022.05.13
[C++] 백준 1753번 - 최단 경로  (0) 2022.05.13
Comments