GameChoi 2023. 1. 21. 17:11

 

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

 

프로그래머스

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

programmers.co.kr

0. Headers

#include <vector>
using namespace std;

1. 알고리즘

1.1 First Algorithm

 - 가로, 세로에서 양쪽 맨 끝의 값을 빼고 곱하게 될 경우 노란색과 값이 같게 됨

   - 따라서 모든 값을 탐색하되 카펫이 가로 세로를 곱한 값이 넘어가는 경우 제외

vector<int> solution(int brown, int yellow) {
    int width = 3;
    while (true)
    {
        for (int height = 3; height <= width; height++)
            if (brown + yellow >= (width) * (height))
                if ((width - 2) * (height - 2) == yellow) return {width, height};
        width++;
    }
}

1.2 Second Algorithm

 - 위의 방법으로 했으나 제출할 때 오류가 생겨 생각해봄

   - 값이 같은데 카펫의 개수보다 작은 경우에 해당하는 값이 생김

     - 10, 10 인경우 (5,4)을 넘기고 (6,4)를 리턴함 (아시는분?????????)

       - 따라서 같은 값인 경우만 조건을 생성

vector<int> solution(int brown, int yellow) {
    int width = 3;
    while (true)
    {
        for (int height = 3; height <= width; height++)
            if (brown + yellow == (width) * (height))
                if ((width - 2) * (height - 2) == yellow) return {width, height};
        width++;
    }
}

2. 완성 코드

#include <vector>
using namespace std;

vector<int> solution(int brown, int yellow) {
    int width = 3;
    while (true)
    {
        for (int height = 3; height <= width; height++)
            if (brown + yellow == (width) * (height))
                if ((width - 2) * (height - 2) == yellow) return {width, height};
        width++;
    }
}