7-1 Score Processing (Java)

7-1 Score Processing (Java)

分数 20

全屏浏览题目

切换布局

作者 翁恺

单位 浙江大学

Write a program to process students score data.

The input of your program has lines of text, in one of the two formats:

  1. Student's name and student id, as <student id>, <name>, and
  2. Score for one student of one course, as <student id>, <course name>, <marks>.

Example of the two formats are:

3190101234, Zhang San
3190101111, Linear Algebra, 89.5

Comma is used as the seperator of each field, and will never be in any of the fields. Notice that there are more than one word for name of the person and name of the course. To make your code easier, the score can be treated as double.

The number of the students and the number of the courses are not known at the beginning. The number of lines are not known at the beginning either. The lines of different format appear in no order. One student may not get enrolled in every course.

The last line has only one word:

END

to indicate the end of the input.

Your program should read every line in and print out a table of summary in .csv format.

The first line of the output is the table head, consists fields like this:

student id, name, <course name 1>, <course name 2>, ..., average

where the course names are all the courses read, in alphabet order. There should be one space after each comma.

Then each line of the output is data for one student, in the ascended order of their student id, with score of each course, like:

3190101234, Zhang San, 85.0, , 89.5, , , 87.3

For the course that hasn't been enrolled, leave a blank before the comma, and should not get included in the average. The average is rounded to one decimal place. There should be one space after each comma.

All the number output, including the averages should be rounded to one decimal place.

Input Format

As described in the text above.

Output Format

As described in the text above.

Sample Input

3180111435, Operating System, 34.5
3180111430, Linear Algebra, 80
3180111435, Jessie Zhao
3180111430, Zhiwen Yang
3180111430, Computer Architecture, 46.5
3180111434, Linear Algebra, 61.5
3180111434, Anna Teng
END

Sample Output

student id, name, Computer Architecture, Linear Algebra, Operating System, average
3180111430, Zhiwen Yang, 46.5, 80.0, , 63.3
3180111434, Anna Teng, , 61.5, , 61.5
3180111435, Jessie Zhao, , , 34.5, 34.5

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

解:

import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        Map<String, Student> hashMap = new HashMap<>();
        HashSet<String> courses = new HashSet<>();
        String get = scanner.nextLine();
        while (!get.equals("END")) {
            String[] strings = get.split(", ");
            int l = strings.length;
//            System.out.println(strings[0]);
            if (!hashMap.containsKey(strings[0])) {

                Student student = new Student(strings[0]);
                if (l == 3) {
                    student.course.put(strings[1], Double.parseDouble(strings[2]));
                    courses.add(strings[1]);
                } else if (l == 2) {
                    student.name = strings[1];
                }

                hashMap.put(strings[0], student);
            } else {

                Student student = hashMap.get(strings[0]);
                if (l == 3) {
                    student.course.put(strings[1], Double.parseDouble(strings[2]));
                    courses.add(strings[1]);
                } else if (l == 2) {
                    student.name = strings[1];
                }
            }
            get = scanner.nextLine();
        }


        System.out.print("student id, name, ");
        ArrayList<String> courseList = new ArrayList<>(courses);
        Collections.sort(courseList);
        String[] course1 = new String[courseList.size()];
        int i=0;
        for (String course : courseList) {
            System.out.print(course+", ");
            course1[i++] = course;
        }
        System.out.println("average");


        Set<String> keySet = hashMap.keySet();
        List<String> list = new ArrayList<>(keySet);
        Collections.sort(list);
        for (String key : list) {
            Student student = hashMap.get(key);
            student.setAverage();
            System.out.print(student.student_id + ", ");
            System.out.print(student.name + ", ");
            for (int j =0;j< course1.length;j++){
                if(student.course.containsKey(course1[j])){
                    System.out.print(student.course.get(course1[j]) + ", ");
                }else {
                    System.out.print(", ");
                }
            }
            System.out.println(String.format("%.1f",student.average));
        }


    }
}

class Student {

    String student_id;
    String name;
    Map<String, Double> course = new HashMap<>();
    double average = 0.0;


    public Student(String student_id) {
        this.student_id = student_id;
    }

    public void setAverage() {
        double sum = 0.0;
        Set<String> set = course.keySet();
        for (String key : set) {
            sum += course.get(key);
        }
        this.average = sum / set.size();

    }

}
 

你提供的`Sort`函数实现了一个简单的冒泡排序算法来按照学生总分从高到低对链表进行排序。代码中使用了结构体成员变量之间的赋值和字符串拷贝操作。 但是你的代码存在一个问题,就是在内层循环中,你没有更新`pr`指针。这会导致每次交换后,`pr`指针都保持不变,而应该向前移动一个位置。 以下是修复这个问题的修改后的代码: ```c void Sort(STU *head) { STU *p, *now, *pr; p = head->next; now = head; pr = head; while (p != NULL) { while (p->next != NULL) { if ((p->score[0] + p->score[1] + p->score[2]) < (p->next->score[0] + p->next->score[1] + p->next->score[2])) { strcpy(pr->num, p->num); strcpy(pr->name, p->name); strcpy(pr->major, p->major); pr->classNo = p->classNo; pr->score[0] = p->score[0]; pr->score[1] = p->score[1]; pr->score[2] = p->score[2]; strcpy(p->num, p->next->num); strcpy(p->name, p->next->name); strcpy(p->major, p->next->major); p->classNo = p->next->classNo; p->score[0] = p->next->score[0]; p->score[1] = p->next->score[1]; p->score[2] = p->next->score[2]; strcpy(p->next->num, pr->num); strcpy(p->next->name, pr->name); strcpy(p->next->major, pr->major); p->next->classNo = pr->classNo; p->next->score[0] = pr->score[0]; p->next->score[1] = pr->score[1]; p->next->score[2] = pr->score[2]; } p = p->next; pr = pr->next; // 添加这一行来更新pr指针 } p = now->next; now = now->next; } } ``` 这样,`pr`指针在内层循环中会向前移动一个位置,确保下一次交换时指向正确的位置。 请记得在调用`Sort`函数时传入合适的参数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值