https://school.programmers.co.kr/learn/courses/30/lessons/42842
yellow의 모든 가로 세로 길이의 수를 돌려가면서 brown, yellow 수를 기반으로 일치하는 가로길이와 세로길이를 구해서 리턴한다.
#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<int> solution(int brown, int yellow) {
for(int i = 1; i <= yellow; i++)
{
int width = yellow / i;
int height = i;
if(height * width == yellow)
if((width + 2) * (height + 2) == (brown + yellow))
return {width + 2, height + 2};
}
}
#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};
}
//////////////////////////////////////
#include <vector>
#include <cmath>
using namespace std;
vector<int> solution(int brown, int yellow) {
int x, y; //2차방정식의 해로 품.
x= 0.5*((2+brown/2)+sqrt((2+brown/2)*(2+brown/2)-4*brown-4*yellow));
y= 0.5*brown-x+2;
return {x,y};
}
위에있는 풀이는
w: 가로, h: 세로, w h = brown + red, (w-2) (h-2) = red, 두 식을 red를 대입해서 연립방정식으로 풀어보면, w = brown/2 + 2 - h, h = n(단, n은 자연수) 가 되는데, h값이 변함에 따라서 w값은 자연스럽게 바뀐다. h가 1 늘어난다는 것은 w가 1 줄어드는 것과 같다.
이렇게 len을 구한 뒤 한 변의 길이는 최소 3이므로 w는 len-3, h는 3부터 시작해서 1씩 가감하면서 일치하는 부분을 구해서 리턴한다.아래있는 풀이는
2차방정식을 계산해서 구했다.