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

 

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

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

 

26548번: Quadratics

The first line will contain a single integer $n$ that indicates the number of data sets that follow. Each data set will contain three numbers (integer or decimal) on a single line, representing $a$, $b$, and $c$ respectively.

www.acmicpc.net

주어지는 이차식의 계수를 문제에 주어져있는 근의 공식에 대입해 두 근을 구하는 문제이다.

 

글쓴이는 long double을 이용하여 문제에서 요구하는 정밀도를 충분히 만족시키는 방향으로 문제를 해결했다.

 

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

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

int T;

ld a, b, c;

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

	cout << fixed;
	cout.precision(3);

	cin >> T;
	while (T--) {
		cin >> a >> b >> c;
		cout << (-b + sqrt(b * b - 4 * a * c)) / (2 * a) << ", " << (-b - sqrt(b * b - 4 * a * c)) / (2 * a) << '\n';
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 5356 // C++] Triangles  (0) 2022.12.21
[BOJ 26534 // C++] Goats  (0) 2022.12.21
[BOJ 24452 // C++] 交易計画 (Trade Plan)  (0) 2022.12.21
[BOJ 26576 // C++] Date  (0) 2022.12.20
[BOJ 26577 // C++] Math  (0) 2022.12.20

+ Recent posts