首页 > 试题广场 >

学分绩点

[编程题]学分绩点
  • 热度指数:9379 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
北京大学对本科生的成绩施行平均学分绩点制(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);)
示例1

输入

5
4 3 4 2 3
91 88 72 69 56

输出

2.52
#include<iostream>
#include<cstdio>
using namespace std;

const int maxn = 11;

int num[maxn];          // 学分

// 将分数换成 GPA
double tgpa(int n)
{
    if(n >= 90 && n <= 100)
    {
        return 4.0;
    }
    else if(n >= 85 && n <= 89)
    {
        return 3.7;
    }
    else if(n >= 82 && n <= 84)
    {
        return 3.3;
    }
    else if(n >= 78 && n <= 81)
    {
        return 3.0;
    }
    else if(n >= 75 && n <= 77)
    {
        return 2.7;
    }
    else if(n >= 72 && n <= 74)
    {
        return 2.3;
    }
    else if(n >= 68 && n <= 71)
    {
        return 2.0;
    }
    else if(n >= 64 && n <= 67)
    {
        return 1.5;
    }
    else if(n >= 60 && n <= 63)
    {
        return 1.0;
    }
    else
    {
        return 0.0;
    }
}

int main()
{
    int n;
    double GPA;         // 平均GPA
    int total;          // 总学分
    int score;          // 成绩
    while(cin >> n)
    {
        GPA = 0;
        total = 0;
        // input
        for(int i = 0; i < n; ++i)
        {
            cin >> num[i];
            total += num[i];
        }
        for(int i = 0; i < n; ++i)
        {
            cin >> score;
            GPA += num[i]*tgpa(score);
        }
        // end input
        printf("%.2lf\n", GPA / total);
    }

    return 0;
}


发表于 2016-08-09 17:08:42 回复(0)
#include<vector>
#include<stdio.h>
using namespace std;

double getGPA(int n){
    if(n>=90)
        return 4.0;
    else if(n>=85&&n<=89)
        return 3.7;
    else if(n>=82&&n<=84)
        return 3.3;
    else if(n>=78&&n<=81)
		return 3.0;
    else if(n>=75&&n<=77)
        return 2.7;
    else if(n>=71&&n<=74)
        return 2.3;
    else if(n>=68&&n<=71)
        return 2.0;
    else if(n>=60&&n<=63)
        return 1.0;
    else
        return 0;
}

int main(){
    int n;
    scanf("%d",&n);
    vector<int> credit(n,0);
    vector<int> score(n,0);
    vector<double> gpas(n,0);
	int totalScore=0;
    for(int i=0;i<n;i++){
       scanf("%d",&credit[i]);
	   totalScore+=credit[i];
    }

    for(int i=0;i<n;i++){
		scanf("%d",&score[i]);
		gpas[i]=getGPA(score[i]);
    }
	
    double totalGpas=0;
    for(int i=0;i<n;i++){
		totalGpas+=gpas[i]*credit[i];
    }
    printf("%.2f\n",totalGpas/totalScore);
    
    return 0;
}


发表于 2016-10-26 15:20:52 回复(0)
#include <iostream>
#include <map>
using namespace std;
int main() {
	map<int, float, greater<int>> s{
		{90,4.0},{85,3.7},{82,3.3},{78,3.0},{75,2.7},
		{72,2.3},{68,2.0},{64,1.5},{60,1.0},{00,0.0} };
	int n, i, a[20], tmp;
	while (cin >> n) {
		float GPA = 0, sum = 0;
		for (i = 0; i < n * 2; ++i)
			cin >> a[i];
		for (i = 0; i < n; ++i) {
			sum += a[i];
			GPA += a[i] * s.lower_bound(a[i + n])->second;
		}
		printf("%.2f\n", GPA / sum);
	}
	return 0;
}

发表于 2021-01-27 06:43:17 回复(0)
#include<stdio.h>
struct chengji{
    float score;
    float jidian;
    float xuefen;
}a[10];
int main()
{
    int n,i;float sum1=0,sum2=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%f",&a[i].xuefen);
    for(i=0;i<n;i++)
        scanf("%f",&a[i].score);
    //1.计算绩点
    for(i=0;i<n;i++)
    {
        if(a[i].score>=90&&a[i].score<=100) a[i].jidian=4.0;
        if(a[i].score>=85&&a[i].score<=89) a[i].jidian=3.7;
        if(a[i].score>=82&&a[i].score<=84) a[i].jidian=3.3;
        if(a[i].score>=78&&a[i].score<=81) a[i].jidian=3.0;
        if(a[i].score>=75&&a[i].score<=77) a[i].jidian=2.7;
        if(a[i].score>=72&&a[i].score<=74) a[i].jidian=2.3;
        if(a[i].score>=68&&a[i].score<=71) a[i].jidian=2.0;
        if(a[i].score>=64&&a[i].score<=67) a[i].jidian=1.5;
        if(a[i].score>=60&&a[i].score<=63) a[i].jidian=1.0;
        if(a[i].score<=59) a[i].jidian=0;
    }
    for(i=0;i<n;i++)
    {sum1+=a[i].jidian*a[i].xuefen;sum2+=a[i].xuefen;}
    float GPA=sum1/sum2;
    printf("%.2f",GPA);
}

发表于 2020-04-02 15:15:35 回复(1)
//博客链接:https://blog.csdn.net/qq_44840148/article/details/104630377
import java.util.Scanner;

public class Main1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
        int n=sc.nextInt(); //输入总共有多少门课程
        int[] gradeCredit=new int[n];//存储每门课学分的数组
        int[] grade=new int[n];//存储每门课分数的数组

        for(int i=0;i<n;i++){
            gradeCredit[i]=sc.nextInt(); //读入输入的第二行
        }
        for(int i=0;i<n;i++){
            grade[i]=sc.nextInt();//读入输入的第三行
        }
        System.out.printf("%.2f",averageCredit(gradeCredit,grade));
    }
}
    public static double averageCredit(int[] gradeCredit,int[] grade){
        double sum=0.0;//表示所有课程加起来应得的学分和
        double allClassCredit=0.0; //表示该学生能实际得到的学分绩点
        for(int i=0;i<grade.length;i++){
            allClassCredit+=gradeCredit[i]*GPA(grade[i]);
            sum+=gradeCredit[i];
        }
        return allClassCredit/sum;
    }
    //求每门课的成绩对应的学分绩点
    public static double GPA(int grade){
        double gpa = 0.0;
        if(grade>=90&&grade<=100)
            gpa=4.0;

        if(grade>=85&&grade<=89)
            gpa=3.7;

        if(grade>=82&&grade<=84)
            gpa=3.3;

        if(grade>=78&&grade<=81)
            gpa=3.0;

        if(grade>=75&&grade<=77)
            gpa=2.7;

        if(grade>=72&&grade<=74)
            gpa=2.3;

        if(grade>=68&&grade<=71)
            gpa=2.0;

        if(grade>=64&&grade<=67)
            gpa=1.5;

        if(grade>=60&&grade<=63)
            gpa=1.0;

        if(grade<60)
            gpa=0.0;

        return gpa;
    }
}

发表于 2020-03-03 13:19:03 回复(0)
#include<bits/stdc++.h>
int main(){
    int n,a[10]={0};
    float b[10]={0},gpa=0,s=0;
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<n;i++){
            scanf("%f",&b[i]);
            if(b[i]>=90 && b[i]<=100) b[i]=4.0;
            else if(b[i]>=85 && b[i]<=89) b[i]=3.7;
            else if(b[i]>=82 && b[i]<=84) b[i]=3.3;
            else if(b[i]>=78 && b[i]<=81) b[i]=3.0;
            else if(b[i]>=75 && b[i]<=77) b[i]=2.7;
            else if(b[i]>=72 && b[i]<=74) b[i]=2.3;
            else if(b[i]>=68 && b[i]<=71) b[i]=2.0;
            else if(b[i]>=64 && b[i]<=67) b[i]=1.5;
            else if(b[i]>=60 && b[i]<=63) b[i]=1.0;
            else b[i]=0;
        }
        for(int i=0;i<n;i++){
            gpa=gpa+a[i]*b[i];
            s=s+a[i];
        }
        printf("%.2f\n",gpa/s);
    }
}
编辑于 2019-03-07 16:09:59 回复(0)
思路很简单,就看题说话就好,就是写起来略繁琐
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
    int n;
    cin >> n;
    int* credit = new int[n];
    int* grade = new int[n];
    for (int i = 0; i<n; i++)
        cin >> credit[i];
    for (int i = 0; i < n; i++)
        cin >> grade[i];
    double gpa = 0;
    for (int i = 0; i<n; i++) {
        if (grade[i] >= 90 && grade[i] <= 100)
            gpa += 4.0*credit[i];
        else if (grade[i] >= 85 && grade[i] <= 89)
            gpa += 3.7*credit[i];
        else if (grade[i] >= 82 && grade[i] <= 84)
            gpa += 3.3*credit[i];
        else if (grade[i] >= 78 && grade[i] <= 81)
            gpa += 3.0*credit[i];
        else if (grade[i] >= 75 && grade[i] <= 77)
            gpa += 2.7*credit[i];
        else if (grade[i] >= 72 && grade[i] <= 74)
            gpa += 2.3*credit[i];
        else if (grade[i] >= 68 && grade[i] <= 71)
            gpa += 2.0*credit[i];
        else if (grade[i] >= 64 && grade[i] <= 67)
            gpa += 1.5*credit[i];
        else if (grade[i] >= 60 && grade[i] <= 63)
            gpa += 1.0*credit[i];
        else
            gpa += 0;
    }
    int totalCredit = 0;
    for (int i = 0; i<n; i++) {
        totalCredit += credit[i];
    }
    cout << fixed << setprecision(2) << gpa / totalCredit << endl;
}

发表于 2019-02-07 17:15:49 回复(1)
while True:
    try:
        num = int(input())
        credits = list(map(int,input().split()))
        scores = list(map(int,input().split()))
        grades = 0
        for i in range(num):
            if 100 >= scores[i] >= 90:
                grades += 4.0*credits[i]
            elif scores[i] >= 85:
                grades += 3.7*credits[i]
            elif scores[i] >= 82:
                grades += 3.3*credits[i]
            elif scores[i] >= 78:
                grades += 3.0*credits[i]
            elif scores[i] >= 75:
                grades += 2.7*credits[i]
            elif scores[i] >= 72:
                grades += 2.3*credits[i]
            elif scores[i] >= 68:
                grades += 2.0*credits[i]
            elif scores[i] >= 64:
                grades += 1.5*credits[i]
            elif scores[i] >= 60:
                grades += 1.0*credits[i]
        print("%.2f"%(grades/sum(credits)))

    except Exception:
        break
编辑于 2018-10-03 00:53:29 回复(0)
//使用结构体 结构体里面设置算绩点的函数
#include <stdio.h>
#include <stdlib.h>
struct Class{
    int xuefen;
    int chengji;
    float getJidian()
    {
        if(chengji >= 90)
            return xuefen * 4.0;
        else if(chengji >= 85)
            return xuefen * 3.7;
        else if(chengji >= 82)
            return xuefen * 3.3;
        else if(chengji >= 78)
            return xuefen * 3.0;
        else if(chengji >= 75)
            return xuefen * 2.7;
        else if(chengji >= 72)
            return xuefen * 2.3;
        else if(chengji >= 68)
            return xuefen * 2.0;
        else if(chengji >= 64)
            return xuefen * 1.5;
        else if(chengji >= 60)
            return xuefen * 1.0;
        else 
            return xuefen * 0.0; 
    }
}buf[10];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        float sum_jidian = 0.0,GPA = 0.0;
        int sum_xuefen=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&buf[i].xuefen);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d",&buf[i].chengji);
        }
        for(int i=0;i<n;i++)
        {
            sum_jidian = sum_jidian + buf[i].getJidian();
            sum_xuefen = sum_xuefen + buf[i].xuefen;
        }
        GPA = sum_jidian / sum_xuefen;
        printf("%.2f\n",GPA);
    }
    return 0;
}

发表于 2018-07-09 10:17:01 回复(1)
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int gradePoint[]=new int[n];//每门课学分
		int coursePoint[]=new int[n];//每门课分数
		
		for(int i=0;i<n;i++){
			gradePoint[i]=in.nextInt();
		}
		for(int i=0;i<n;i++){
			coursePoint[i]=in.nextInt();
		}
		in.close();
		System.out.printf("%.2f",gradePointAverage(gradePoint,coursePoint));//格式输出
		
	}
	public static double pointOfEachCourse(int grade){//根据实际成绩判断学分方法
		double point = 0.0;
		if(grade>=90&&grade<=100)
			point=4.0;
		
		if(grade>=85&&grade<=89)
			point=3.7;
		
		if(grade>=82&&grade<=84)
			point=3.3;
		
		if(grade>=78&&grade<=81)
			point=3.0;
		
		if(grade>=75&&grade<=77)
			point=2.7;
		
		if(grade>=72&&grade<=74)
			point=2.3;
		
		if(grade>=68&&grade<=71)
			point=2.0;
		
		if(grade>=64&&grade<=67)
			point=1.5;
		
		if(grade>=60&&grade<=63)
			point=1.0;
		
		if(grade<60)
			point=0.0;
		
		return (double)point;
	}
	//计算平均绩点的方法
	public static double gradePointAverage(int[] gradePoint,int[] coursePoint){
		double gradePointTotal=0;
		double gradeOfEachCourseTotal=0;
		for(int i=0;i<gradePoint.length;i++){
			gradePointTotal+=pointOfEachCourse(coursePoint[i])*gradePoint[i];
			gradeOfEachCourseTotal+=gradePoint[i];
		}
		return (double)gradePointTotal/gradeOfEachCourseTotal;
	}
}

发表于 2016-04-06 21:06:01 回复(0)

python solution

60分以下是0不是0.1,千万别看错了。。妈的,查了半天最后发现这里错了。

代码通过:

a,b,c=input(),list(map(int,input().split())),list(map(int,input().split()))
total=0
for i,v in enumerate(c):
    if 90<=v<=100:total+=b[i]*4
    elif 85<=v<=89:total+=b[i]*3.7
    elif 82<=v<=84:total+=b[i]*3.3
    elif 78<=v<=81:total+=b[i]*3
    elif 75<=v<=77:total+=b[i]*2.7
    elif 72<=v<=74:total+=b[i]*2.3
    elif 68<=v<=71:total+=b[i]*2
    elif 64<=v<=67:total+=b[i]*1.5
    elif 60<=v<=63:total+=b[i]*1
    else:total+=b[i]*0
print("{0:.2f}".format(total/sum(b)))
发表于 2017-10-16 17:12:55 回复(7)
!!  60分以下的是0!  不是0.1!
!!  60分以下的是0!  不是0.1!
!!  60分以下的是0!  不是0.1!
#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int main() {
    int x,arr[10],arr1[10];
    scanf("%d", &x);
    if (x>0&&x<10)
    {
        for (int i = 0; i < x; i++)
        {
            scanf("%d", &arr1[i]); //学分
        }
        for (int i = 0; i < x; i++)
        {
            scanf("%d", &arr[i]);  //得分 -
        }
        float sum=0,sum1=0, num[10];
        for (int i = 0; i < x; i++)
        {
            if (90<=arr[i] &&arr[i]<=100)
            {
                num[i] = 4.0;
            }
            else if (85<= arr[i] && arr[i] <= 89)
            {
                num[i] = 3.7;
            }else if (82 <= arr[i] && arr[i] <= 84)
            {
                num[i] = 3.3;
            }
            else if (78 <= arr[i] && arr[i] <= 81)
            {
                num[i] = 3.00;
            }
            else if (75 <= arr[i] && arr[i] <= 77)
            {
                num[i] = 2.70;
            }
            else if (72<= arr[i] && arr[i] <= 74)
            {
                num[i] = 2.30;
            }
            else if (68 <= arr[i] && arr[i] <= 71)
            {
                num[i] = 2.00;
            }
            else if (64 <= arr[i] && arr[i] <= 67)
            {
                num[i] = 1.50;
            }
            else if (60<= arr[i] && arr[i] <= 63)
            {
                num[i] = 1;
            }
            else
            {
                num[i] = 0.0;
            }
        } //绩点
        for (int i = 0; i < x; i++)
        {
            sum += num[i]*arr1[i]; //绩点之和
            sum1 =sum1+ arr1[i]; //课程学分
        }
        printf("%.2f", sum/sum1);
    }
     return 0;
}
编辑于 2024-02-29 16:55:09 回复(0)
这题有毒吧,我本地得出的就是2.68,上传上去就成了2.86,篡改我答案?
另外这题就一个用例。
d = {60:0,64:1,68:1.5,72:2,75:2.3,78:2.7,82:3,85:3.3,90:3.7,101:4}
a,b,j,p = int(input()),list(map(int,input().split())),-1,0
for i in map(int,input().split()):
    j,k = j + 1,0
    while i >= list(d)[k]:
        k += 1
    p += d[list(d)[k]] * b[j]
print('{:.2f}'.format(p / sum(b)))


发表于 2020-03-24 18:45:44 回复(0)
Java 解法
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] credit = new int[n];
        int[] score = new int[n];
        // 总的学分
        int credits=0;
        for (int i = 0; i < n; i++){
            credit[i]=scanner.nextInt();
            credits+= credit[i];
        }
        for (int i = 0; i < n; i++)
            score[i]= scanner.nextInt();
        double average =0;
        for (int i = 0; i < n; i++)
            average+= scoreToGPA(score[i])*credit[i];
        DecimalFormat df = new DecimalFormat(".00");
        System.out.println(df.format(average/credits));
    }

    /**
     *  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
     * */
    static double scoreToGPA(int score){
       if (score>=90) return 4.0;
       else if (score>=85) return 3.7;
       else if (score>=82) return 3.3;
       else if (score>=78) return 3.0;
       else if (score>=75) return 2.7;
       else if (score>=72) return 2.3;
       else if (score>=68) return 2.0;
       else if (score>=64) return 1.5;
       else if (score>=60) return 1.0;
       else return 0.0;
    }
}


发表于 2020-03-13 16:13:46 回复(0)
注意:60以下学分是0,不是0.1。
编辑于 2024-03-24 20:25:03 回复(0)
/*
100 - 90  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
0 - 60 0
*/
#include<vector>
#include<iostream>
using namespace std;
float calCredit(int num){
      if(num>=90){
         return 4.0;
      }else if(num>=85){
         return 3.7;
      }else if(num>=82){
        return 3.3;
      }else if(num>=78){
        return 3.0;
      }else if(num>=75){
        return 2.7;
      }else if(num>=72)
      return 2.3;
      else if(num>=68){
        return 2.0;
      }else if(num>=64){
        return 1.5;
      }else if(num>=60){
        return 1.0;
      }else{
        return 0;
      }
}
int main(){
    int N;
    int allCredits = 0;
    scanf("%d",&N);
    vector<int> credits;
    vector<int> grades;
    for(int i=0;i<N;i++){
        int c;
        cin>>c;
        allCredits+=c;
        credits.push_back(c);
    }
    for(int i=0;i<N;i++){
        int g;
        cin>>g;
        grades.push_back(g);
    }
    float res1 = 0;
    for(int i=0;i<N;i++){
       float tmp =  calCredit(grades[i])*credits[i];
       res1+=tmp;
    }
    float GPA = res1/allCredits;
    printf("%.2f\n",GPA);
}

发表于 2024-03-24 16:11:25 回复(0)
#include <iomanip>
#include <iostream>
using namespace std;

struct sub{
    int xuefen;
    int defen;
}sub[12];

int main() {
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>sub[i].xuefen;
    }
    for(int i=0;i<n;i++){
        cin>>sub[i].defen;
    }

    double jidian[12];
    double xuefenjidian[12];

    for(int i=0;i<n;i++){
        if(sub[i].defen>=90&&sub[i].defen<=100) jidian[i]=4.0;
        else if(sub[i].defen>=85&&sub[i].defen<=89) jidian[i]=3.7;
        else if(sub[i].defen>=82&&sub[i].defen<=84) jidian[i]=3.3;
        else if(sub[i].defen>=78&&sub[i].defen<=81) jidian[i]=3.0;
        else if(sub[i].defen>=75&&sub[i].defen<=77) jidian[i]=2.7;
        else if(sub[i].defen>=72&&sub[i].defen<=74) jidian[i]=2.3;
        else if(sub[i].defen>=68&&sub[i].defen<=71) jidian[i]=2.0;
        else if(sub[i].defen>=64&&sub[i].defen<=67) jidian[i]=1.5;
        else if(sub[i].defen>=60&&sub[i].defen<=63) jidian[i]=1.0;
        else jidian[i]=0;
    }
    for(int i=0;i<n;i++){
        xuefenjidian[i]=jidian[i]*sub[i].xuefen;
    }

    double z1=0;//总绩点
    int z2=0;//总学分

    for (int i=0;i<n;i++){
        z1+=xuefenjidian[i];
    }
    for(int i=0;i<n;i++){
        z2+=sub[i].xuefen;
    }

    double GPA=z1/z2;
    cout<<fixed<<setprecision(2)<<GPA;
}
// 64 位输出请用 printf("%lld")

编辑于 2024-03-10 17:34:54 回复(0)
#include <iomanip>
#include <ios>
#include <iostream>
#include <vector>
using namespace std;

//根据实际成绩计算绩点
double gpa(int grade) {
    return grade >= 90 ? 4.0 :
           grade >= 85 ? 3.7 :
           grade >= 82 ? 3.3 :
           grade >= 78 ? 3.0 :
           grade >= 75 ? 2.7 :
           grade >= 72 ? 2.3 :
           grade >= 68 ? 2.0 :
           grade >= 64 ? 1.5 :
           grade >= 60 ? 1.0 : 0;
}

int main() {
    int n;
    while (cin >> n) {
        vector<int>course_score;    //每门课程的学分
        int sum_score = 0;          //所有课程学分总和
        for (int i = 0; i < n; i++) {
            int score;      //课程学分
            cin >> score;
            sum_score += score;
            course_score.push_back(score);
        }
        double sum_gpa = 0; //所有学科绩点之和
        for (int i = 0; i < n; i++) {
            int grade;  //实际成绩
            cin >> grade;
            sum_gpa += gpa(grade) * course_score[i];
        }
        //输出总评绩点,精确到小数点后2位
        cout << setiosflags(ios::fixed) << setprecision(2)
             << sum_gpa / sum_score << endl;
    }
    return 0;
}

编辑于 2024-02-01 11:49:29 回复(0)
本机c语言环境下结果正确,为什么在这里计算有偏差呢,求大神帮忙解答一下
#include <stdio.h>

struct subject{
     int point;
     int score;
};
int main() {
    int n;
    scanf("%d",&n);
    float sum=0;
    float gpa;
    struct subject s[n];
    for(int i=0;i<n;i++){
        scanf("%d",&s[i].point);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&s[i].score);
    }
    for(int i=0;i<n;i++){
        if(s[i].score>=90&&s[i].score<=100){
            sum+=4.0*s[i].point;
        }else if (s[i].score>=85&&s[i].score<=89){
            sum+=3.7*s[i].point;
        }else if (s[i].score>=82&&s[i].score<=84){
            sum+=3.3*s[i].point;
        }
        else if (s[i].score>=78&&s[i].score<=81){
            sum+=3.0*s[i].point;
        }else if (s[i].score>=75&&s[i].score<=77){
            sum+=2.7*s[i].point;
        }else if (s[i].score>=72&&s[i].score<=74){
            sum+=2.3*s[i].point;
        }else if (s[i].score>=68&&s[i].score<=71){
            sum+=2.0*s[i].point;
        }else if (s[i].score>=64&&s[i].score<=67){
            sum+=1.5*s[i].point;
        }else if (s[i].score>=60&&s[i].score<=63){
            sum+=1.0*s[i].point;
        }else if(s[i].point<60){
            sum+=0;
        }

    }
    int pointall;
    for(int i=0;i<n;i++){
        pointall+=s[i].point;
    }
    gpa=sum/pointall;
    printf("%.2f",gpa);
    return 0;
}

发表于 2023-10-26 15:17:28 回复(0)
#include <cstdio>

float getGradeClass(int grade){
    float gradeclass;
    if( 90<=grade && grade<=100){
        gradeclass = 4.0;
    }else if(85<=grade && grade<=89){
        gradeclass = 3.7;
    }else if(82<=grade && grade<=84){
        gradeclass = 3.3;
    }else if(78<=grade && grade<=81){
        gradeclass = 3.0;
    }else if(75<=grade && grade<=77){
        gradeclass = 2.7;
    }else if(72<=grade && grade<=74){
        gradeclass = 2.3;
    }else if(68<=grade && grade<=71){
        gradeclass = 2.0;
    }else if(64<=grade && grade<=67){
        gradeclass = 1.5;
    }else if(60<=grade && grade<=63){
        gradeclass = 1.0;
    }else{
        gradeclass = 0;
    }
    return gradeclass;
}

int main(){
    int n;
    scanf("%d",&n);
    int credit[10];//学分
    int sum_credit = 0;//总学分
    int grade[10];//单科成绩
    float gradeclass[10];//成绩对应的绩点
    float subjectclass[10];//学科绩点 = 成绩对应绩点*学分
    float sum_subjectclass = 0;//学科绩点之和
    //输入学分
    for(int i = 0; i < n; ++i){
        scanf("%d",&credit[i]);
        sum_credit += credit[i];
    }
    //输入成绩
    for(int i = 0; i < n; ++i){
        scanf("%d",&grade[i]);
        gradeclass[i] = getGradeClass(grade[i]);
        subjectclass[i] = gradeclass[i]*credit[i];
        sum_subjectclass += subjectclass[i];
    }

    float GPA = sum_subjectclass/sum_credit;
    printf("%0.2f",GPA);
    return 0;
}

发表于 2023-03-10 19:04:08 回复(0)