1297번: TV 크기
김탑은 TV를 사러 인터넷 쇼핑몰에 들어갔다. 쇼핑을 하던 중에, TV의 크기는 그 TV의 대각선의 길이로 나타낸 다는 것을 알았다. 하지만, 김탑은 대각선의 길이가 같다고 해도, 실제 TV의 크기는
www.acmicpc.net
오랜만에 풀어보는 중학수학이었는데 처음에 문제도 잘못 이해하고 수학 공식들을 리마인드 하느라 시간이 조금 걸렸다.
피타고라스의 정리를 사용하면 공식을 찾을 수 있는데 공식대로 x = √d^2 / (w^2 + h^2) 을 입력하면 오답이다. 문제에서 높이와 너비의 비율을 제시하는데 대각선의 실제 길이보다 비율의 값이 더 클 수 있기 때문이다. 이럴 경우 해당 공식으로는 답을 구할 수 없다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace _230208_1
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
int[] arr = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
int d = arr[0]; // 대각선
int h = arr[1]; // 세로 비율
int w = arr[2]; // 가로 비율
double x = Math.Sqrt((d * d) / ((w * w) + (h * h))); // 비율을 구하는 공식
sw.WriteLine("{0} {1}", Math.Truncate(x * h), Math.Truncate(x * w));
sr.Close();
sw.Close();
}
}
}
- 오답
어차피 w와 h가 100을 넘지 못하는 상황에서 해당 변수들을 무조건 100으로 나눠줘도 비율은 같게되고 절대로 d를 넘지 못하도록 수를 제한할 수 있다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace _230208_1
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
int[] arr = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
int d = arr[0]; // 대각선
double h = arr[1]; // 세로 비율
double w = arr[2]; // 가로 비율
h /= 100;
w /= 100;
double x = Math.Sqrt((d * d) / ((w * w) + (h * h))); // 비율을 구하는 공식
sw.WriteLine("{0} {1}", Math.Truncate(x * h), Math.Truncate(x * w));
sr.Close();
sw.Close();
}
}
}
Math.Truncate() -> 입력값의 내림 값을 구하는 메서드
Math.Sqrt() -> 입력값의 제곱근을 구하는 메서드
'Algorithm > BOJ' 카테고리의 다른 글
C#) [BOJ] 2178 미로 탐색 ★ (0) | 2023.02.10 |
---|---|
C#) [BOJ] 2108 통계학 ★ (0) | 2023.02.09 |
C#)[BOJ] 1764 듣보잡 ★ (0) | 2023.02.06 |
C#) [BOJ] 2581 소수 (0) | 2023.02.06 |
C#) [BOJ] 1260 DFS와 BFS ★ (0) | 2023.02.05 |