본문 바로가기

problem solving

[프로그래머스]카펫

주어진 red의 가로와 세로를 구한다면 brown의 가로, 세로 길이를 구할 수 있다.
red의 길이를 구하기 위해 가능한 가로, 세로의 모든 경우의 수를 store배열에 저장한다.
저장된 store의 배열을 이용해 store에 저장된 가로와 세로의 길이를 이용해
(가로길이 * 2 + (세로길이 + 2) * 2)의 값이 brown이 되면 이 때의 가로, 세로 길이가 답이다.

 

#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
vector<int> solution(int brown, int red) {
    vector<int> answer(2);
    
    int row = 0, col = 1;
    vector<pair<intint>> store;
    for(int idx = 0; idx < brown; idx++) {
        row++;
        col = 0;
        while(row * col < red) {
            col++;
        }
        if(row * col != red) { continue; }
        store.push_back( { row, col } );
    }
    
    for(int idx = 0; idx < store.size(); idx++) {
        if(store[idx].first * 2 + (store[idx].second + 2* 2 != brown) { continue; }
        answer[0= store[idx].second + 2, answer[1= store[idx].first + 2;
        break;
    }
    
    return answer;
}
cs

'problem solving' 카테고리의 다른 글

[프로그래머스]단어 변환  (0) 2020.03.12
[프로그래머스]네트워크  (0) 2020.03.12
[프로그래머스]베스트앨범  (0) 2020.03.12
[프로그래머스]위장  (0) 2020.03.12
[프로그래머스]구명보트  (0) 2020.03.11