https://www.welcomekakao.com/learn/courses/30/lessons/42842


모든 격자 수의 합이 갈색과 빨간색의 합이 된다.


곱해서 합이 되는 경우를 구하면 카펫의 가로와 세로를 구할 수 있고, 구한 값에 대해서 가운데에


빨간색 격자의 수를 감당할 수 있는지 확인해준다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#pragma warning(disable :4996)
#include<iostream>
#include<vector>
using namespace std;
 
int Sum;
vector<int> solution(int br, int red) {
    vector<int> ans;
 
    Sum = br + red; //전체 격자의 수
    int h; //높이
    for (int w = Sum; w >= 1; w--) {
 
        if (Sum % w == 0) {
            h = Sum / w;
            if (w < h) break//가로가 세로보다 길거나 같을때만 보면됨
            if ((w - 2)*(h - 2== red) {
                ans.push_back(w);
                ans.push_back(h);
            }
        }
    }
    return ans;
}
 
cs


+ Recent posts