< 문제링크 >
https://www.welcomekakao.com/learn/courses/30/lessons/42842
< 풀이 >
1. 노란 영역을 기준으로 가로길이를 row, 세로길이를 col 이라고 하면...
- brown = 2*row + 2*yellow + 4
- yellow = row * col
2. col을 1부터 해답이 나올 때 까지 증가시킨다.
-> 이 때, row = ((brown - 4) - (2*yellow)) / 2 이다.
< 코드 >
#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<int> solution(int brown, int yellow) {
vector<int> answer;
// yellow 영역을 기준으로 row와 col을 사용
int col = 1, row;
while (true) {
// col = 1 일 때, brown 값으로 row 계산 가능
// brown = 2*row + 2*col + 4
row = ((brown - 4) - (2 * col)) / 2;
// yellow 영역을 구한 것과 일치하면 정답
if (row*col == yellow) {
// 주의할점은 카펫 전체의 길이이므로 각각 2를 더해주어야 함
answer.push_back(row+2);
answer.push_back(col+2);
break;
}
col++;
}
return answer;
}
int main(void)
{
int brown = 10;
int yellow = 2;
vector<int> answer = solution(brown, yellow);
cout << answer[0] << ", " << answer[1] << endl;
return 0;
}
'알고리즘 > Programmers' 카테고리의 다른 글
Programmers / BFS, DFS / 네트워크 (0) | 2020.09.09 |
---|---|
Programmers / BFS, DFS / 타겟 넘버 (0) | 2020.09.09 |
Programmers / 완전탐색 / 소수 판별 (0) | 2020.09.08 |
Programmers / 완전탐색 / 모의고사 (0) | 2020.09.08 |
Programmers / Sort / H-index (0) | 2020.09.07 |