정화 코딩

[C++] 이번학기 평점은 몇점? 본문

PS

[C++] 이번학기 평점은 몇점?

jungh150c 2024. 9. 16. 04:08

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

 

#include <iostream>
#include <vector>
using namespace std;

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

    int n;
    cin >> n;
    int cnt = 0;
    double sums = 0;
    while (n--) {
        string name, grade;
        int weight;
        double score = 0;
        cin >> name >> weight >> grade;

        if (grade == "A+") score = 4.3;
        else if (grade == "A0") score = 4.0;
        else if (grade == "A-") score = 3.7;
        else if (grade == "B+") score = 3.3;
        else if (grade == "B0") score = 3.0;
        else if (grade == "B-") score = 2.7;
        else if (grade == "C+") score = 2.3;
        else if (grade == "C0") score = 2.0;
        else if (grade == "C-") score = 1.7;
        else if (grade == "D+") score = 1.3;
        else if (grade == "D0") score = 1.0;
        else if (grade == "D-") score = 0.7;
        else if (grade == "F") score = 0.0;

        sums += weight * score;
        cnt += weight;
    }

    cout << fixed;
    cout.precision(2);
    cout << (sums / cnt + 0.005) << '\n';
}

(WA)

 

#include <iostream>
#include <vector>
using namespace std;

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

    int n;
    cin >> n;
    int cnt = 0;
    double sums = 0;
    while (n--) {
        string name, grade;
        int weight;
        double score = 0;
        cin >> name >> weight >> grade;

        if (grade == "A+") score = 4.3;
        else if (grade == "A0") score = 4.0;
        else if (grade == "A-") score = 3.7;
        else if (grade == "B+") score = 3.3;
        else if (grade == "B0") score = 3.0;
        else if (grade == "B-") score = 2.7;
        else if (grade == "C+") score = 2.3;
        else if (grade == "C0") score = 2.0;
        else if (grade == "C-") score = 1.7;
        else if (grade == "D+") score = 1.3;
        else if (grade == "D0") score = 1.0;
        else if (grade == "D-") score = 0.7;
        else if (grade == "F") score = 0.0;

        sums += weight * score;
        cnt += weight;
    }

    cout << fixed;
    cout.precision(2);
    cout << (sums / cnt + 0.000001) << '\n';
}

(AC)

왜 cout << (sums / cnt + 0.005) << '\n'; 이건 맞고 cout << (sums / cnt + 0.000001) << '\n'; 이건 틀리는지 아직 명확하게 이해가 안 된다... 부동소수점과 반올림에 대해서 더 공부해야 할 듯.

 

'PS' 카테고리의 다른 글

[C++] 용액 (백준 2467번)  (1) 2024.09.19
[C++] 구슬 탈출 2 (백준 13460번)  (0) 2024.09.19
[C++] 부분합 (백준 1806번)  (0) 2024.09.15
[C++] 파티 (백준 1238번)  (0) 2024.09.15
[C++] Four Squares (백준 17626번)  (0) 2024.09.12
Comments