九度OJ 1133:学分绩点 (加权平均数)
- 题目描述:
-
北京大学对本科生的成绩施行平均学分绩点制(GPA)。既将学生的实际考分根据不同的学科的不同学分按一定的公式进行计算。
公式如下:
实际成绩 绩点
90——100 4.0
85——89 3.7
82——84 3.3
78——81 3.0
75——77 2.7
72——74 2.3
68——71 2.0
64——67 1.5
60——63 1.0
60以下 0
1.一门课程的学分绩点=该课绩点*该课学分
2.总评绩点=所有学科绩点之和/所有课程学分之和
现要求你编写程序求出某人A的总评绩点(GPA)。
- 输入:
-
第一行 总的课程数n(n<10);
第二行 相应课程的学分(两个学分间用空格隔开);
第三行 对应课程的实际得分;
此处输入的所有数字均为整数。
- 输出:
-
输出有一行,总评绩点,精确到小数点后2位小数。(printf("%.2f",GPA);)
- 样例输入:
-
5 4 3 4 2 3 91 88 72 69 56
- 样例输出:
-
2.52
思路:
实际上就是求加权平均数。
代码:
#include <stdio.h> #include <string.h> float getPoint(int x) { if (90 <= x && x <= 100) return 4.0; else if (85 <= x && x <= 89) return 3.7; else if (82 <= x && x <= 84) return 3.3; else if (78 <= x && x <= 81) return 3.0; else if (75 <= x && x <= 77) return 2.7; else if (72 <= x && x <= 74) return 2.3; else if (68 <= x && x <= 71) return 2.0; else if (64 <= x && x <= 67) return 1.5; else if (60 <= x && x <= 63) return 1.0; else return 0; } int main(void) { int cre[10], score[10]; int n, i, sumCre; float sumPoint; while (scanf("%d", &n) != EOF) { for (i=0; i<n; i++) scanf("%d", &cre[i]); for (i=0; i<n; i++) scanf("%d", &score[i]); sumPoint = 0; sumCre = 0; for (i=0; i<n; i++) { sumPoint += cre[i] * getPoint(score[i]); sumCre += cre[i]; } printf("%.2f\n", sumPoint/sumCre); } return 0; } /************************************************************** Problem: 1133 User: liangrx06 Language: C Result: Accepted Time:0 ms Memory:912 kb ****************************************************************/
编程算法爱好者。