※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 10187번 문제인 Golden이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/10187 

 

10187번: Golden

There are many special irrational numbers in nature. One is the “golden ratio” represented by the Greek letter phi, φ, which has a value of φ = (1 + √5) / 2 = 1.61803399..... Given 2 real numbers, determine whether their ratio is equal (or close to

www.acmicpc.net

주어진 두 수의 비가 황금비의 0.99배와 1.01배의 값 사이에 있는지를 판단하는 것으로 문제를 해결하자.

 

단, y값이 0으로 입력이 들어오면 0으로 나누기를 시도하게 되므로 적절한 예외처리를 해주자.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <cmath>
using namespace std;
typedef long double ld;

int T;
ld gr = (1 + sqrt((ld)5)) / 2;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> T;
	while (T--) {
		ld x, y; cin >> x >> y;
		if (y == 0) cout << "not\n";
		else {
			ld val = x / y;
			if (val < gr * 0.99 || gr * 1.01 < val) cout << "not\n";
			else cout << "golden\n";
		}
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 26552 // C++] Zero  (0) 2022.12.21
[BOJ 26535 // C++] Chicken Pen  (0) 2022.12.21
[BOJ 26502 // C++] Decoder  (0) 2022.12.21
[BOJ 26536 // C++] Cowspeak  (0) 2022.12.21
[BOJ 26560 // C++] Period  (0) 2022.12.21

+ Recent posts