본문 바로가기
알고리즘/프로그래머스

[ 프로그래머스 / 완전탐색 ] 카펫

by 뎁꼼 2020. 6. 30.

1. 문제


 

 

코딩테스트 연습 - 카펫

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

programmers.co.kr

 

 

2. 소스코드


- w를 고정, h를 w까지 늘려가며 비교했다.

- 연립방정식을 세워 풀면, while 문 하나로도 가능하다.

 

소스코드

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

vector<int> solution(int brown, int yellow) {
    vector<int> answer;
    int h=3, w=3;
    long long int temp_brown = 0;
    
    while(true){
        h = 3;
        while(h <= w){
            temp_brown = w * 2 + (h - 2) * 2;
           
            if(temp_brown == brown and w * h - temp_brown == yellow){
                answer.push_back(w);
                answer.push_back(h);
                return answer;
            }
            h++;
        }
        w++;
    }
}