[프로그래머스 C++] 카펫

 

https://school.programmers.co.kr/learn/courses/30/lessons/42842

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


 

 

해결전략

 

완전 탐색

 


 

코드

 

#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> solution(int brown, int yellow) {
    vector<int> answer;

    int total = brown + yellow;
    int row = 0, column = 0, dif = total;

    for (int i=1; i<= total/2; i++)
    {
	    if (total % i == 0) //약수인 경우
        {
            int temp = total / i;//i와 대응되는 약수 구하기

            if(dif > abs(temp - i)) //약수 쌍의 차이값이 작은 것을 사용
            {
                // 가능한 행, 열에서 노란색 타일이 적절하게 배치되는지 확인
                int yellow_tile_count = (temp - 2) * (i - 2);

                if (yellow_tile_count == yellow) 
                {
                    dif = abs(temp - i);
                    column = temp;
                    row = i;
                }
            }
	    }
    }

    answer.push_back(column);
    answer.push_back(row);

    return answer;
}

int main(){

    for (auto i : solution(24, 24))
        cout << i << " ";
    

    return 0;
}