프로그래머스/2레벨
카펫/C++
Koalitsiya
2023. 1. 6. 16:22
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 모든 격자의 수의 합은 brown + red, 카펫의 가로, 세로 길이는 최소 3이상이다.
2. 반복문을 돌리며 width에 sum을 height로 나눈 몫을 대입한다.
3. sum이 height로 나누어 떨어지고 (width -2)*(height-2) 즉 테두리를 1줄을 제외한 부분의 값이 yellow와 같으면 answer에 width와 height를 담고 break한다.
4. answer 리턴
#include <string>
#include <vector>
using namespace std;
vector<int> solution(int brown, int yellow) {
vector<int> answer;
int sum = brown + yellow;
for(int height = 3;;height++){
int width = sum / height;
if(!(sum % height) && (width - 2)*(height - 2) == yellow) {
answer.push_back(width);
answer.push_back(height);
break;
}
}
return answer;
}