코딩 공부 연습

프로그래머스 카펫

miffy짱 2022. 8. 11. 12:57
반응형

레벨2 문제였는데 쉬웠다 캌ㅋ

 

 

#include <string>
#include <vector>

using namespace std;

vector<int> solution(int brown, int yellow) {
    vector<int> answer;
    
    //x * y를 했을 때 그 값이 brown + yellow 인 x, y 쌍중에서, 2x+2y -4 하면 brown 인 쌍 찾기?
    int sum = brown + yellow;
    for (int i = 1; i < sum/2; i++)
    {
        if (sum%i == 0)
        {
            int tmp1 = i;
            int tmp2 = sum/i;
            if (2*(tmp1 + tmp2) -4 == brown)
            {
                if (tmp1 > tmp2)
                {    
                    answer.push_back(tmp1); answer.push_back(tmp2);
                    return answer;
                }
                else
                {
                    answer.push_back(tmp2); answer.push_back(tmp1);
                    return answer;
                }
            }
        }
    }
    return answer;
}