알고리즘/[문제풀이] 프로그래머스

[프로그래머스/고득점 Kit/완전탐색] 카펫

be-lgreen 2021. 10. 7. 00:49

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

 

코딩테스트 연습 - 카펫

Leo는 카펫을 사러 갔다가 아래 그림과 같이 중앙에는 노란색으로 칠해져 있고 테두리 1줄은 갈색으로 칠해져 있는 격자 모양 카펫을 봤습니다. Leo는 집으로 돌아와서 아까 본 카펫의 노란색과

programmers.co.kr

 

난이도

완전탐색 / 난이도 ⭐️/ 10분

 

문제풀이

#include <string>
#include <vector>

using namespace std;

vector<int> solution(int brown, int yellow) {
    vector<int> answer;
    
    // 1 24 => 26*2 + 3*2 - 4 = 54
    // 2 12 => 8 + 28 - 4 = 32
    // 3 8 => 10 + 20 - 4 = 26
    // 4 6 => 12 + 16 - 4 = 24
    
    //약수 구하기
    vector<int> v;
    for(int i=1; i<=yellow; i++){
        if(yellow % i == 0)
            v.push_back(i);
    }
    
    int a, b, c;
    for(int i=0; i<v.size(); i++){
        a = v[i];
        b = yellow / a;
        c = (a+2) * 2 + (b+2) * 2 - 4;
        
        if(c == brown){
            if(a >= b){
                answer.push_back(a + 2);
                answer.push_back(b + 2);
            }else{
                answer.push_back(b + 2);
                answer.push_back(a + 2);
            }
            
            break;
        }
    }
    
    return answer;
}