문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/42842
코딩테스트 연습 - 카펫
Leo는 카펫을 사러 갔다가 아래 그림과 같이 중앙에는 노란색으로 칠해져 있고 테두리 1줄은 갈색으로 칠해져 있는 격자 모양 카펫을 봤습니다. Leo는 집으로 돌아와서 아까 본 카펫의 노란색과
programmers.co.kr
아래의 공식만 이해하면 쉽게 풀 수 있는 문제였습니다.
- 가로 * 세로 = (갈색 + 노란색)
- (가로 - 2) * (세로 - 2) = 노란색
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
vector<int> solution(int brown, int yellow) { | |
vector<int> answer; | |
int total = brown + yellow; | |
for (int row = 1; row < total; row++) | |
{ | |
if (total % row) | |
{ | |
continue; | |
} | |
int col = total / row; | |
if ((row - 2) * (col - 2) == yellow) | |
{ | |
answer = { max(row, col), min(row, col) }; | |
break; | |
} | |
} | |
return answer; | |
} | |
int main(void) | |
{ | |
int brown = 24; | |
int yellow = 24; | |
vector<int> lens = solution(brown, yellow); | |
for (int len : lens) | |
{ | |
cout << len << " "; | |
} | |
cout << "\n"; | |
return 0; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers 위클리 챌린지 7주차] 입실 퇴실 (0) | 2021.09.14 |
---|---|
[Programmers 코딩테스트 고득점 Kit] 기능개발 (0) | 2021.09.12 |
[Programmers 코딩테스트 고득점 Kit] 소수 찾기 (0) | 2021.09.12 |
[Programmers 코딩테스트 고득점 Kit] 모의고사 (0) | 2021.09.12 |
[Programmers 코딩테스트 고득점 Kit] 여행경로 (0) | 2021.09.11 |