PAT Basic 1015. 德才论
PAT Basic 1015. 德才论
1. 题目描述:
宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”
现给出一批考生的德才分数,请根据司马光的理论给出录取排名。
2. 输入格式:
输入第一行给出 3 个正整数,分别为:
随后 准考证号 德分 才分
,其中准考证号
为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。
3. 输出格式:
输出第一行首先给出达到最低分数线的考生人数
4. 输入样例:
14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60
5. 输出样例:
12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90
6. 性能要求:
Code Size Limit
16 KB
Time Limit
400 ms
Memory Limit
64 MB
思路:
考察结构体的使用和排序算法,因为最后要输出每位考生的三种信息,所以定义一个结构体方便排序和输出。
这里我第一次做是定义了一个结构体,然后因为考生被分为四类,我就分别定义了四个结构体数组(比较浪费空间的还是,看了别人的题解才发现根据类别定义个权重量用于排序就好),然后对四个结构体数组分别进行冒泡排序,最后好像是一直有测试点报timeout。。。
然后查到了一种比较巧妙的解法,这里注释掉的第二个解法,根本没定义结构体,直接把考生的三种信息加类别共四种信息融合成了一个值,进行冒泡排序,最后再从这个融合值分解出三种信息进行输出。
最后就是一种常规的解法,好像也是直接copy别人的,把compare函数写清楚,然后用qsort()库函数进行快速排序。
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
这里也可以参考大佬的解法:PAT-Basic-1015. 德才论 – Lnyan's Blog (llonely.com)
My Code:
// #include <stdio.h>
// #include <stdlib.h>
// struct students
// {
// int id;
// int de;
// int cai;
// };
// void stuBubbleSort(struct students * stuList, int number);
// int main(void) // my first solution
// {
// int stuNum = 0;
// int lowestLine = 0;
// int betterLine = 0;
// struct students *stuList1, *stuList2, *stuList3, *stuList4;
// int count1 = 0, count2 = 0, count3 = 0, count4 = 0;
// int tempId = 0, tempDe = 0, tempCai = 0;
// scanf("%d%d%d", &stuNum, &lowestLine, &betterLine);
// stuList1 = (struct students *)malloc(sizeof(struct students) * stuNum);
// stuList2 = (struct students *)malloc(sizeof(struct students) * stuNum);
// stuList3 = (struct students *)malloc(sizeof(struct students) * stuNum);
// stuList4 = (struct students *)malloc(sizeof(struct students) * stuNum);
// for(int i=0; i<stuNum; i++)
// {
// scanf("%d%d%d", &tempId, &tempDe, &tempCai);
// if(tempDe >= betterLine && tempCai >= betterLine)
// {
// stuList1[count1].id = tempId;
// stuList1[count1].de = tempDe;
// stuList1[count1].cai = tempCai;
// count1++;
// }
// else if(tempDe >= betterLine && tempCai >= lowestLine)
// {
// stuList2[count2].id = tempId;
// stuList2[count2].de = tempDe;
// stuList2[count2].cai = tempCai;
// count2++;
// }
// else if(tempDe >= lowestLine && tempCai >= lowestLine && tempDe >= tempCai)
// {
// stuList3[count3].id = tempId;
// stuList3[count3].de = tempDe;
// stuList3[count3].cai = tempCai;
// count3++;
// }
// else if(tempDe >= lowestLine && tempCai >= lowestLine)
// {
// stuList4[count4].id = tempId;
// stuList4[count4].de = tempDe;
// stuList4[count4].cai = tempCai;
// count4++;
// }
// }
// printf("%d", count1+count2+count3+count4);
// if(count1) stuBubbleSort(stuList1, count1);
// if(count2) stuBubbleSort(stuList2, count2);
// if(count3) stuBubbleSort(stuList3, count3);
// if(count4) stuBubbleSort(stuList4, count4);
// return 0;
// }
// void stuBubbleSort(struct students * stuList, int number)
// {
// struct students temp;
// int flag = 0;
// for(int i=0; i<number-1; i++)
// {
// flag = 0;
// for(int j=number-2; j>=i ; j--)
// {
// if((stuList[j].de + stuList[j].cai) < (stuList[j+1].de + stuList[j+1].cai))
// {
// temp = stuList[j];
// stuList[j] = stuList[j+1];
// stuList[j+1] = temp;
// flag = 1;
// }
// else if((stuList[j].de+stuList[j].cai == stuList[j+1].de+stuList[j+1].cai) && (stuList[j].de < stuList[j+1].de))
// {
// temp = stuList[j];
// stuList[j] = stuList[j+1];
// stuList[j+1] = temp;
// flag = 1;
// }
// else if((stuList[j].de+stuList[j].cai == stuList[j+1].de+stuList[j+1].cai) && (stuList[j].de == stuList[j+1].de) && (stuList[j].id>stuList[j+1].id))
// {
// temp = stuList[j];
// stuList[j] = stuList[j+1];
// stuList[j+1] = temp;
// flag = 1;
// }
// }
// if(!flag) break;
// }
// for(int i = 0; i < number; i++)
// {
// printf("\n%d %d %d", stuList[i].id, stuList[i].de, stuList[i].cai);
// }
// }
// #include<stdio.h>
// #define l 100000000000000
// #define s 100000000000
// #define d 100000000
// int main() // other's solution, clever thinking.
// {
// int N, L, H;
// scanf("%d %d %d", &N, &L, &H);
// int count = 0, level, D, C, sum, kaohao;
// long long student[N], temp;
// for(int i = 0; i < N; i++){
// scanf("%d %d %d", &kaohao, &D, &C);
// //判断分数所属情况
// if(D >= L && C >= L){
// sum = D + C;
// if(D >= H && C >= H){
// level = 4;
// }
// else if(D >= H){
// level = 3;
// }
// else if(D >= C){
// level = 2;
// }
// else{
// level = 1;
// }
// student[count] = level * l + sum * s + D * d + d - kaohao; //将分数信息保存在数组
// count++;
// }
// }
// //排序
// for(int i = 0; i < count; i++){
// for(int j = i; j < count; j++){
// if(student[i] < student[j]){
// temp = student[i];
// student[i] = student[j];
// student[j] = temp;
// }
// }
// }
// //输出
// printf("%d\n", count);
// for(int i = 0; i < count; i++){
// kaohao = d - student[i] % d;
// sum = student[i] % l / s;
// D = student[i] % s / d;
// C = sum - D;
// printf("%d %d %d\n", kaohao, D, C);
// }
// return 0;
// }
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct studentNode{
int id,type;
int c;
int d;
int total;
} student;
int compare(void *a,void *b){
student *pa=(student *)a;
student *pb=(student *)b;
if(pa->type!=pb->type)return pb->type-pa->type;//如果考生权重不同直接返回右边减去左边的权重,权重大的一方排序在前
else if(pa->total!=pb->total)return pb->total-pa->total;//如果权重相同,看总分排名,总分大的一方在前
else if(pa->d!=pb->d)return pb->d-pa->d;//如果总分相同,德分高的一方在在前
else return pa->id-pb->id;//德分相同,考号小的一方在前
}
int main(){
int n,primeL,advancedL,i,count=0;//count标记合格考生,primeL为合格线,advancedL为优秀线
scanf("%d %d %d",&n,&primeL,&advancedL);
student st[n];
for(i=0;i<n;i++){
scanf("%d %d %d",&st[i].id,&st[i].d,&st[i].c);
st[i].total=st[i].c+st[i].d;
if(st[i].d<primeL||st[i].c<primeL){//不合格考生
st[i].type=0;
}else if(st[i].d>=advancedL&&st[i].c>=advancedL){//1类考生权重5
st[i].type=5;
count++;
}else if(st[i].d>=advancedL&&st[i].c<advancedL){//2类考生权重4
st[i].type=4;
count++;
}else if(st[i].d>=st[i].c){//三类考生权重3
st[i].type=3;
count++;
}else{//四类考生权重2
st[i].type=2;
count++;
}
}
qsort(st,n,sizeof(student),compare);
printf("%d",count);
for(i=0;i<count;i++){
printf("\n%d %d %d",st[i].id,st[i].d,st[i].c);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端