콘솔창 & 윈도우창/코딩 테스트

[백준 실버5] 수들의 합5 2018

게임 개발 2024. 10. 7. 14:05

 

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

 

 

 

투포인터 쓰기

처음에 무지성 포문 생각했는데

조금 생각해보니 포문으론 안되는 걸 알았다.

 

 

#include <string>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>

using namespace std;


int main(void)
{

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

    int n, count = 1, startIdx = 1, endIdx = 1, sum = 1;
    
    cin >> n;

    while (endIdx != n)
    {
        if (sum == n) {
            count++;
            endIdx++;
            sum = sum + endIdx;
        }
        else if (sum > n) {
            sum = sum - startIdx;
            startIdx++;
        }
        else {
            endIdx++;
            sum = sum + endIdx;
        }
    }
    cout << count << "\n";


    return 0;
}