题解 | #学分绩点#

学分绩点

https://www.nowcoder.com/practice/a0c09a7e0da04e728810a8aca7226b7b

#include<bits/stdc++.h>
using namespace std;

//获取每门课程的学分
void get_credit(int n, vector<float>& credit) {
    for (int i = 0; i < n; i++) {
        float c;
        cin >> c;
        credit.push_back(c);
    }
}

//获取每门课程的成绩
void get_score(int n, vector<float>& score) {
    for (int i = 0; i < n; i++) {
        float s;
        cin >> s;
        score.push_back(s);
    }
}

int main() {
    int course_num;//课程总数
    cin >> course_num;
    vector<float> credit;//vector数组存储每门课程的学分
    vector<float> score;//vector数组存储每门课程的成绩
    get_credit(course_num, credit);
    get_score(course_num, score);

    //计算所有学科绩点之和以及所有课程学分之和
    float sum = 0;
    float sum_credit = 0;
    for (int i = 0; i < course_num; i++) {
        sum_credit += credit[i];
        if (score[i] >= 90 && score[i] <= 100) {
            sum += 4.0 * credit[i];
        } else if (score[i] >= 85 && score[i] <= 89) {
            sum += 3.7 * credit[i];
        } else if (score[i] >= 82 && score[i] <= 84) {
            sum += 3.3 * credit[i];
        } else if (score[i] >= 78 && score[i] <= 81) {
            sum += 3.0 * credit[i];
        } else if (score[i] >= 75 && score[i] <= 77) {
            sum += 2.7 * credit[i];
        } else if (score[i] >= 72 && score[i] <= 74) {
            sum += 2.3 * credit[i];
        } else if (score[i] >= 68 && score[i] <= 71) {
            sum += 2.0 * credit[i];
        } else if (score[i] >= 64 && score[i] <= 67) {
            sum += 1.5 * credit[i];
        } else if (score[i] >= 60 && score[i] <= 63) {
            sum += 1.0 * credit[i];
        } else {
            sum += 0;
        }
    }

    //计算总评绩点
    float gpa = sum / sum_credit;
    cout << setprecision(3) << gpa << endl; //保留两位小数
}

全部评论

相关推荐

joe2333:怀念以前大家拿华为当保底的日子
点赞 评论 收藏
分享
M_bao:简历排版换一下吧,第二个项目换了吧,咱门双非学历本来就不行还用这种项目太掉分了,300沟通一个要简历你打招呼也有问题。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务