😈 알고리즘/💻 백준
💻 1008번 문제 : A/B 📕🙋
Buᐢ༝ᐢy
2022. 9. 18. 17:54
1008번: A/B


#include <iostream>
using namespace std;
int main()
{
double A, B;
cin >> A >> B;
cout << fixed;
cout.precision(9);
cout << A / B;
return 0;
}
메모리 (KB) | 시간 (ms) | 코드 길이 (B) |
2020 | 0 | 167 |
이 외에도 정답으로 인정됐던 코드 모음
#include <iostream> using namespace std; int main() { double A, B; cin >> A >> B; cout.precision(10); cout << A / B; return 0; }
#include <iostream> #include <iomanip> using namespace std; int main() { std::cout << std::setprecision(16); long double A, B; cin >> A >> B; cout << A / B; return 0; }
📕 오답 노트
#include <iostream>
using namespace std;
int main()
{
float A, B;
cin >> A >> B;
cout << A / B;
return 0;
}
틀렸던 코드

→ 소수 9번째 자리까지만 출력하는 건 아니지만 상대오차가 그 이하면 정답
🙋 더 알아보기
float, double, long double 차이를 정확히 알자
실수 자료형 | 바이트 | 출력 크기 | 유효 자릿수 |
float | 4 | 1.175494351 E - 38 ~ 3.402823466 E + 38 | 6 - 7 |
double | 8 | 2.2250738585072014 E - 308 ~ 1.7976931348623158 E + 308 | 15 - 16 |
long double | 8 | 2.2250738585072014 E - 308 ~ 1.7976931348623158 E + 308 | 15 - 16 |
float 형식
부동 소수점 수는 IEEE(Institute of Electrical and Electronics Engineers) 형식을 사용합니다. float 형식의 단정밀도 값은 부호 비트, -127승 이진 지수 8비트 및 가수 23비트로 구성된 4바이트를 사용합니다. 가수는 1.0에서 2.0 사이의 수를 의미합니다. 가수의 상위 비트가 항상 1이기 때문에 가수의 상위 비트는 수에 저장되지 않습니다.

#include <iostream>
using namespace std;
int main()
{
float A, B;
cin >> A >> B;
cout.precision(9);
cout << fixed;
cout << A / B;
return 0;
}
// 결과
// 0.333333343
#include <iostream>
using namespace std;
int main()
{
double A, B;
cin >> A >> B;
cout.precision(9);
cout << fixed;
cout << A / B;
return 0;
}
// 결과
// 0.333333333
실수형 자릿수 표현하기
#include <iostream>
using namespace std;
int main()
{
double A, B;
cin >> A >> B;
cout << A / B;
return 0;
}
// 결과
// 0.333333
위 코드와 결과를 보면 알 수 있듯이, float, double 상관 없이 소수점 6자리까지만 출력한다. 문제에 맞게끔 소수점 9자리까지 출력해야 한다.
cout.precision(9);
: 소수점 9자리까지 출력하도록 해줌
cout << fixed;
: precision(n)에서 지정한 자릿수까지 소수점 출력
setprecision - C++ Reference
Sets the decimal precision to be used to format floating-point values on output operations. Behaves as if member were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams ). This manipulator is declared in header .
fixed - C++ Reference
ios_base& fixed (ios_base& str); Use fixed floating-point notation Sets the floatfield format flag for the str stream to fixed. Whenfloatfield is set to fixed, floating-point values are written using fixed-point notation: the value is represented with exactly as many digits in the decimal part as specified by the precision field () and with no exponent part.
Uploaded by N2T