1484 - 다이어트

2025. 2. 17. 13:12PS/백준

문제

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

느낀점

골드 이상부터는 바로 어떻게 풀어야겠다는 생각이 들지 않는 경우가 많기 때문에 고민을 좀 해야한다. 다행히도 이 문제는 조건이나 이런 것들이 쉬웠던 듯

풀이

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;

int main()
{
    int G;
    cin >> G;
    int left = 1, right = 1;
    vector<int> answer;
    bool finded = false;
    while (true)
    {
        int comp = pow(right, 2) - pow(left, 2);
        if (comp < G)
        {
            right++;
        }
        else
        {
            if (comp == G)
            {
                answer.push_back(right);
                finded = true;
            }
            if (right - left == 1 && comp > G)
            {
                break;
            }

            left++;
        }
    }
    if (finded)
    {
        sort(answer.begin(), answer.end());
        for (size_t i = 0; i < answer.size(); i++)
        {
            cout << answer[i] << "\n";
        }
    }
    else
    {
        cout << -1;
    }

    return 0;
}

'PS > 백준' 카테고리의 다른 글

2230 - 수 고르기  (0) 2025.02.17
2118 - 두 개의 탑  (0) 2025.02.17
25916 - 싫은데요  (0) 2025.02.17
20922 - 겹치는 건 싫어  (0) 2025.02.17
15565 - 귀여운 라이언  (0) 2025.02.17