😈 알고리즘/💻 백준

💻 1008번 문제 : A/B 📕🙋

Buᐢ༝ᐢy 2022. 9. 18. 17:54
1008번: A/B
https://www.acmicpc.net/problem/1008
#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)
20200167
  • 이 외에도 정답으로 인정됐던 코드 모음
    #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 차이를 정확히 알자

실수 자료형바이트출력 크기유효 자릿수
float41.175494351 E - 38 ~ 3.402823466 E + 386 - 7
double82.2250738585072014 E - 308 ~ 1.7976931348623158 E + 30815 - 16
long double82.2250738585072014 E - 308 ~ 1.7976931348623158 E + 30815 - 16
float 형식
부동 소수점 수는 IEEE(Institute of Electrical and Electronics Engineers) 형식을 사용합니다. float 형식의 단정밀도 값은 부호 비트, -127승 이진 지수 8비트 및 가수 23비트로 구성된 4바이트를 사용합니다. 가수는 1.0에서 2.0 사이의 수를 의미합니다. 가수의 상위 비트가 항상 1이기 때문에 가수의 상위 비트는 수에 저장되지 않습니다.
https://learn.microsoft.com/ko-kr/cpp/c-language/type-float?view=msvc-170

#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 .
https://cplusplus.com/reference/iomanip/setprecision/
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.
https://cplusplus.com/reference/ios/fixed/


Uploaded by N2T