백준

2018번 (silver5)

이야기prog 2025. 1. 11. 19:18

https://www.acmicpc.net/problem/2018

간단한 투포인터 알고리즘을 사용한 문제이다.

 

#include <iostream>

using namespace std;
int main() {

	long N, start = 1, end = 1, count = 0;
	cin >> N;
	while (end <= N) {
		long sum = (start + end) * (end + 1 - start) / 2;
		if (sum > N) start++;
		else if (sum < N) end++;
		else count++, end++;
	}
	cout << count;
	

	return 0;
}

투포인터의 기본을 이해하면 쉽게 풀이가 가능하다.