4级学分加权平均绩点倒序

7-6 4级学分加权平均绩点倒序
分数 10
作者 冯筠
单位 西北大学
假设百分制课程成绩和4分制绩点对应计算方法如下:

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* 绩点1)+(学分2* 绩点2).]/(学分1+学分2.)

输入若干个学生学号(4位数字)和若干门课的百分制成绩和对应学分,按照学生的学分加权平均绩点倒序输出学号和加权平均绩点。以*为结束

输入格式:
学号 百分制成绩 学分 每行一个,以-1为结束记号。

输出格式:
按平均绩点从高到低输出:
学号 平均绩点,每行一个,绩点保留2位小数。

输入样例:
在这里给出一组输入。例如:

1001 98 3
1002 89 3
1003 78 3
1004 76 3
1001 98 2
1002 89 2
1003 78 3
1004 76 3
1001 65 2
1002 57 2
1003 78 2
1004 98 2
1001 98 3
1002 89 3
1003 78 3
1002 76 1
-1
输出样例:
在这里给出相应的输出。例如:

1001 3.50
1004 3.03
1003 3.00
1002 2.94

using System;

namespace GPA
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] GPA = new double[100];  // 存储每个学生的绩点
            int[] credit = new int[100];     // 存储每个学生每门课的学分
            int n = 0;                       // 学生数量

            // 输入学生学号和成绩和学分
            while (true)
            {
                int id, score, credit_i;
                Console.Write("请输入学号:");
                id = int.Parse(Console.ReadLine());
                if (id == -1) break;

                Console.Write("请输入百分制成绩:");
                score = int.Parse(Console.ReadLine());

                Console.Write("请输入学分:");
                credit_i = int.Parse(Console.ReadLine());

                // 根据成绩计算绩点
                double gpa;
                if (score >= 90) gpa = 4.0;
                else if (score >= 85) gpa = 3.7;
                else if (score >= 82) gpa = 3.3;
                else if (score >= 78) gpa = 3.0;
                else if (score >= 75) gpa = 2.7;
                else if (score >= 72) gpa = 2.3;
                else if (score >= 68) gpa = 2.0;
                else if (score >= 64) gpa = 1.5;
                else if (score >= 60) gpa = 1.0;
                else gpa = 0;

                // 存储学生的绩点和学分
                if (n == 0)
                {
                    GPA[0] = gpa * credit_i;
                    credit[0] = credit_i;
                    n++;
                }
                else
                {
                    // 检查是否已经输入该学生的成绩
                    bool found = false;
                    for (int i = 0; i < n; i++)
                    {
                        if (id == i + 1)
                        {
                            found = true;
                            GPA[i] += gpa * credit_i;
                            credit[i] += credit_i;
                            break;
                        }
                    }
                    if (!found)
                    {
                        GPA[n] = gpa * credit_i;
                        credit[n] = credit_i;
                        n++;
                    }
                }
            }

            // 按平均绩点从高到低排序并输出结果
            Console.WriteLine("按平均绩点从高到低输出:");
            for (int i = 0; i < n; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (GPA[i] / credit[i] < GPA[j] / credit[j])
                    {
                        double t_GPA = GPA[i];
                        GPA[i] = GPA[j];
                        GPA[j] = t_GPA;

                        int t_credit = credit[i];
                        credit[i] = credit[j];
                        credit[j] = t_credit;
                    }
                }

                Console.WriteLine("{0} {1:F2}", i + 1, GPA[i] / credit[i]);
            }

        }
    }
}