[백준 1297] TV 크기 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
TV의 대각선 길이와 비율(종횡비)을 바탕으로 실제 세로 및 가로 길이를 계산하는 문제입니다.
- TV의 대각선 길이
D
와 종횡비H:W
가 주어집니다. - 실제 화면 크기는 세로
H'
, 가로W'
로 주어져야 하며,
주어진 비율을 유지하면서도 \(H'^2 + W'^2 = D^2\) 를 만족해야 합니다.
접근법
- 입력으로 주어지는 종횡비
H:W
는 비례값일 뿐, 실제 길이는 아니므로
피타고라스 정리를 통해 비례 상수를 곱해 실제 길이를 계산해야 합니다. -
삼각형에서 대각선
\[\text{scale} = \frac{D}{\sqrt{h^2 + w^2}}\]D
, 종횡비h:w
를 생각하면 다음이 성립합니다: -
이 비례 상수를 곱해서 실제 세로, 가로 길이를 구합니다:
\[H' = \left\lfloor h \times \text{scale} \right\rfloor,\quad W' = \left\lfloor w \times \text{scale} \right\rfloor\]
Code
[ C# ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
class Program {
static void Main() {
var input = Console.ReadLine().Split();
double d = double.Parse(input[0]);
double h = double.Parse(input[1]);
double w = double.Parse(input[2]);
double scale = d / Math.Sqrt(h * h + w * w);
int ansH = (int)(h * scale);
int ansW = (int)(w * scale);
Console.WriteLine($"{ansH} {ansW}");
}
}
[ C++ ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
double d, h, w; cin >> d >> h >> w;
double ratio = sqrt(h * h + w * w);
int ansH = h * d / ratio, ansW = w * d / ratio;
cout << ansH << " " << ansW << "\n";
return 0;
}