프로그래머스 C#/Lv.2
[프로그래머스 C#] 카펫
썩은피망
2024. 6. 9. 20:43
반응형
문제 살펴보기
- 노란색 칸의 가로는 x, 세로는 y라고 가정했을 때 갈색 칸의 가로는 x+2, 세로는 y+2입니다.
- x * y = yellow
- (x+2) * (y+2) = brown + yellow
- 위 조건을 이용하여 가로와 세로 크기를 구하세요.
제한사항
- 8 ≤ brown ≤ 5,000
- 1 ≤ yellow ≤ 2,000,000
- 가로 길이는 세로 길이와 같거나, 세로 길이보다 깁니다.
입출력 예
brown | yellow | result |
10 | 2 | [4, 3] |
8 | 1 | [3, 3] |
24 | 24 | [8, 6] |
using System;
using System.Collections.Generic;
public class Solution {
public int[] solution(int brown, int yellow) {
int[] answer = new int[] {};
List<(int, int)> t = temp(yellow);
foreach(var v in t)
{
int size = (v.Item1 + 2) * (v.Item2 + 2);
if(size == brown + yellow) return new int[2]{v.Item1 + 2, v.Item2 + 2};
}
return answer;
}
List<(int, int)> temp(int yellow)
{
List<(int, int)> result = new List<(int, int)>();
for(int y = 1; y <= yellow; y++)
{
if(yellow % y != 0) continue;
int x = yellow / y;
if(x >= y) result.Add((x, y));
}
return result;
}
}
반응형