PAT 1083 List Grades [排序]

本文介绍了一种算法,用于处理学生记录数据,包括姓名、ID和成绩,并按成绩降序排序,输出指定成绩区间内的学生记录。示例输入包含学生信息及成绩范围,输出为该范围内成绩的学生记录。

Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval.

Input Specification:

Each input file contains one test case. Each case is given in the following format:

N
name[1] ID[1] grade[1]
name[2] ID[2] grade[2]
... ...
name[N] ID[N] grade[N]
grade1 grade2

where name[i] and ID[i] are strings of no more than 10 characters with no space, grade[i] is an integer in [0, 100], grade1 and grade2 are the boundaries of the grade's interval. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case you should output the student records of which the grades are in the given interval [grade1grade2] and are in non-increasing order. Each student record occupies a line with the student's name and ID, separated by one space. If there is no student's grade in that interval, output NONE instead.

Sample Input 1:

4
Tom CS000001 59
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95
60 100

Sample Output 1:

Mike CS991301
Mary EE990830
Joe Math990112

Sample Input 2:

2
Jean AA980920 60
Ann CS01 80
90 95

Sample Output 2:

NONE

-----------------------------------我是题目和解题的分割线----------------------------------- 

#include<cstdio>
#include<algorithm>

using namespace std;

struct node
{
	char name[11]; //末尾有\0不要只开成10
	char id[11];
	int grade;
}stu[10005];

bool cmp(node a,node b)
{
	return a.grade>b.grade;
}

int main()
{
	int n,i,j;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%s%s%d",stu[i].name,stu[i].id,&stu[i].grade);
	sort(stu,stu+n,cmp);
	int a,b,flag = 0;
	scanf("%d%d",&a,&b);
	for(i=0;i<n;i++)
	{
		if(stu[i].grade>=a&&stu[i].grade<=b)
		{
			printf("%s %s\n",stu[i].name,stu[i].id);
			flag = 1;
		}	
	}
	if(!flag) printf("NONE\n");
}

 

### Java中基于两个字段对List进行排序 对于Java而言,在处理`List`集合时,如果要依据两个字段来进行排序操作,则可以利用`Comparator`接口配合Lambda表达式或是方法引用的方式完成定制化比较逻辑。下面给出一段具体的代码示例用于说明如何实现这一功能: ```java import java.util.*; class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } // Getters and setters omitted for brevity @Override public String toString() { return "Person{name='" + name + "', age=" + age + "}"; } } public class Main { public static void main(String[] args) { List<Person> people = Arrays.asList( new Person("Alice", 30), new Person("Bob", 25), new Person("Charlie", 35), new Person("David", 25) ); // Sort by age first then by name alphabetically. people.sort(Comparator.comparingInt((Person p) -> p.getAge()) .thenComparing(p -> p.getName())); System.out.println(people); } } ``` 这段程序首先创建了一个包含若干个人员记录的列表,接着调用了`sort()`方法并传入了复合型的`Comparator`作为参数。这里先是按年龄升序排列,当两个人拥有相同的年龄时再按照姓名字母顺序进一步区分先后次序[^1]。 ### Python中基于两个字段对List进行排序 而在Python里,同样能够轻松达成同样的效果。可以通过给定的关键字参数key指定一个返回待排序项属性值的函数来控制排序行为;若需考虑多个条件则可借助元组组合这些属性值一并考量。下面是相应的例子: ```python from operator import attrgetter class Student: def __init__(self, name, grade, score): self.name = name self.grade = grade self.score = score def __repr__(self): return f'Student({self.name}, {self.grade}, {self.score})' students = [ Student('Tom', 'A', 98), Student('Jerry', 'B', 76), Student('Spike', 'C', 85), Student('Tyke', 'B', 76) ] # Sorting students list primarily based on grade descendingly, # secondarily according to scores ascendingly within same grades. sorted_students = sorted(students, key=lambda s: (-attrgetter('grade')(s), s.score)) print(sorted_students) ``` 此段脚本展示了怎样先依照年级降序安排学生的位置,之后在同一级别内部依分数从小到大调整位置关系[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值