프로그래머스 문제 풀이

완전탐색 - 카펫

란서 2021. 1. 7. 23:21

<생각의 흐름>

 

 

옐로우가 주어지면 나눈다.

 

1) 옐로우가 N이면 너비 = N부터 시작해서 나눴을 나머지가 0 몫만 Y 집어 넣는다.

 

높이 =

 

그러면 너비와 높이 만들어진다.

 

(너비+2)*2 + (높이+2) 갈색의 수와 맞다면

 

가로 세로는 너비+2, 높이+2 된다.

 

 

<코드 구현>

#include <string>

#include <vector>

#include <cmath>

using namespace std;



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

    vector<int> answer;

   

    int width;

    int height;

    int match;

    for(int i=yellow; i>0; --i) {

        if(!(yellow % i)) {

            width = i;

            height = yellow/i;

            if(width < height) continue;

            match = (width+2)*2 + height*2;

            if(match == brown) {

                answer.push_back(width+2);

                answer.push_back(height+2);

                break;

            }

         }

    }

    return answer;

}

 

-다른 사람 풀이

#include <string>
#include <vector>
using namespace std;
vector<int> solution(int brown, int red) {
int len = brown / 2 + 2;
int w = len - 3;
    int h = 3;
while(w >= h){
        if(w * h == (brown + red)) break;
w--;
        h++;
    }
return vector<int>{w, h};
}

//출처: <https://programmers.co.kr/learn/courses/30/lessons/42842/solution_groups?language=cpp> 

 

'프로그래머스 문제 풀이' 카테고리의 다른 글

[힙(heap)] 더 맵게  (0) 2021.02.04
DFS/BFS 여행 경로  (0) 2021.01.26
DFS/BFS 단어변환  (0) 2021.01.20
DFS/BFS 네트워크  (0) 2021.01.11
DFS/BFS 타겟 넘버  (0) 2021.01.09