题解 | #学分绩点#
学分绩点
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; //保留两位小数 }