[백준 2858] 기숙사 바닥 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
테두리 타일 수와 내부 타일 수가 주어졌을 때 방의 크기를 구하는 문제입니다.
접근법
전체 타일 수는 빨간색과 갈색을 합한 값이고, 이는 방의 가로와 세로를 곱한 값과 같습니다.
전체 타일 수의 약수 쌍을 탐색하며, 테두리 타일 수 조건을 만족하는 크기를 찾아 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
class Program {
static void Main() {
var parts = Console.ReadLine()!.Split();
var r = int.Parse(parts[0]);
var b = int.Parse(parts[1]);
var total = r + b;
for (var w = 1; w * w <= total; w++) {
if (total % w != 0) continue;
var l = total / w;
if (2 * l + 2 * w - 4 == r) {
Console.WriteLine($"{l} {w}");
return;
}
}
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int r, b; cin >> r >> b;
int total = r + b;
for (int w = 1; w * w <= total; w++) {
if (total % w != 0) continue;
int l = total / w;
if (2 * l + 2 * w - 4 == r) {
cout << l << " " << w << "\n";
return 0;
}
}
return 0;
}