PAT_(排序)1028 1055 1075 1083

目录

1028 List Sorting (25分)

1055 The World's Richest (25分)

1075 PAT Judge (25分)

1083 List Grades (25分)


 

1028 List Sorting (25分)

Excel can sort records according to any column. Now you are supposed to imitate this function.

Input Specification:

Each input file contains one test case. For each case, the first line contains two integers N (≤10​5​​) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student's record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

Output Specification:

For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID's; if C= 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID's in increasing order.

Sample Input 1:

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

Sample Output 1:

000001 Zoe 60
000007 James 85
000010 Amy 90

写3个cmp函数

分别是 按准考证,姓名,分数,从小到大排序,

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=100010; //注意
struct node{
	int id;
	char name[15];
	int score;
}stu[maxn];
bool cmp1(node a,node b){
	return a.id<b.id; //id从小到大 
}
bool cmp2(node a,node b){
	 int ans=strcmp(a.name,b.name);
	 if(ans!=0) return ans<0;
	 else return a.id<b.id;
}
bool cmp3(node a,node b){
	if(a.score!=b.score) return a.score<b.score;
	else return a.id<b.id;
}

int main(){
	int N,C;
	scanf("%d%d",&N, &C);
	for(int i=0;i<N;i++){
		scanf("%d %s %d",&stu[i].id, stu[i].name, &stu[i].score);
	}
	if(C==1){
		sort(stu,stu+N,cmp1);
	}else if(C==2){
		sort(stu,stu+N,cmp2);
	}else if(C==3){
		sort(stu,stu+N,cmp3);
	}
	for(int i=0;i<N;i++){
		printf("%06d %s %d\n",stu[i].id,stu[i].name,stu[i].score); //id前面的0不要忘了 
	}
	return 0;
}

 

 

 

1055 The World's Richest (25分)

Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a certain range of ages. That is, given the net worths of N people, you must find the M richest people in a given range of their ages.

N (≤10​^5​​)  K (≤10^​3​​)  

注意:preprocessing操作,应为m<100 所以每个age的100名往后的人永远不会被输出,我们只保留每个age的前100名 

Sample Input:

12 4
Zoe_Bill 35 2333
Bob_Volk 24 5888
Anny_Cin 95 999999
Williams 30 -22
Cindy 76 76000
Alice 18 88888
Joe_Mike 32 3222
Michael 5 300000
Rosemary 40 5888
Dobby 24 5888
Billy 24 5888
Nobody 5 0
4 15 45
4 30 35
4 5 95
1 45 50

Sample Output:

Case #1:
Alice 18 88888
Billy 24 5888
Bob_Volk 24 5888
Dobby 24 5888
Case #2:
Joe_Mike 32 3222
Zoe_Bill 35 2333
Williams 30 -22
Case #3:
Anny_Cin 95 999999
Michael 5 300000
Alice 18 88888
Cindy 76 76000
Case #4:
None

 

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
const int N=100010;
struct node{
	int age,asset;
	char name[12];
}p[N],valid[N];
int Age[N]; //某年龄的人数 
bool cmp(node a,node b){
	if(a.asset!=b.asset) return a.asset>b.asset;
	else if(a.age!=b.age)return a.age<b.age;
	else return strcmp(a.name,b.name)<0;
} 
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++){
		scanf("%s%d%d",p[i].name,&p[i].age,&p[i].asset);
	}
	sort(p,p+n,cmp);
	int validnum=0;
	for(int i=0;i<n;i++){
		if(Age[p[i].age]<100){  //preprocessing
			Age[p[i].age]++;
			valid[validnum++]=p[i];
		}	
	}
	int m,l,r;
	for(int i=1;i<=k;i++){
		scanf("%d%d%d",&m,&l,&r);
		printf("Case #%d:\n",i);
		int Num=0;
		for(int j=0;j<validnum&&Num<m;j++){
			if(valid[j].age>=l && valid[j].age<=r){
				printf("%s %d %d\n",valid[j].name,valid[j].age,valid[j].asset); 
				Num++; 
			}	
		}
		if(Num==0) printf("None\n");		
	}	
	return 0;
}

 

1075 PAT Judge (25分)

The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 positive integers, N (≤10​^4​​), the total number of users, K (≤5), the total number of problems, and M (≤10^​5​​), the total number of submissions. It is then assumed that the user id's are 5-digit numbers from 00001 to N, and the problem id's are from 1 to K. The next line contains K positive integers p[i] (i=1, ..., K), where p[i] corresponds to the full mark of the i-th problem. Then M lines follow, each gives the information of a submission in the following format:

user_id problem_id partial_score_obtained

where partial_score_obtained is either −1 if the submission cannot even pass the compiler, or is an integer in the range [0, p[problem_id]]. All the numbers in a line are separated by a space.

Output Specification:

For each test case, you are supposed to output the ranklist in the following format:

rank user_id total_score s[1] ... s[K]

where rank is calculated according to the total_score, and all the users with the same total_score obtain the same rank; and s[i] is the partial score obtained for the i-th problem. If a user has never submitted a solution for a problem, then "-" must be printed at the corresponding position. If a user has submitted several solutions to solve one problem, then the highest score will be counted.

The ranklist must be printed in non-decreasing order of the ranks. For those who have the same rank, users must be sorted in nonincreasing order according to the number of perfectly solved problems. And if there is still a tie, then they must be printed in increasing order of their id's. For those who has never submitted any solution that can pass the compiler, or has never submitted any solution, they must NOT be shown on the ranklist. It is guaranteed that at least one user can be shown on the ranklist.

Sample Input:

7 4 20
20 25 25 30
00002 2 12
00007 4 17
00005 1 19
00007 2 25
00005 1 20
00002 2 2
00005 1 15
00001 1 18
00004 3 25
00002 2 25
00005 3 22
00006 4 -1
00001 2 18
00002 1 20
00004 1 15
00002 4 18
00001 3 4
00001 4 2
00005 2 -1
00004 2 0

Sample Output:

1 00002 63 20 25 - 18
2 00005 42 20 0 22 -
2 00007 42 - 25 - 17
2 00001 42 18 18 4 2
5 00004 40 15 0 25 -

模拟pat的判题,样例解释:7个人,4道题,每道题20 25 25 30,共20次提交记录,返回排名(有提交记录的)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
const int N=10010; 
struct node{
	int id;
	int score[6];
	bool flag; //是否有通过编译的提交记录 
	int sum;//总分
	int solve; //解题数 
}p[N];
int n,k,m;
int full[6];
bool cmp(node a,node b){
	if(a.sum!=b.sum) return a.sum>b.sum;
	else if(a.solve!=b.solve) return a.solve>b.solve;
	else return a.id<b.id; 	
}
void init(){
	for(int i=1;i<=n;i++){
		p[i].id=i;
		p[i].sum=0;
		p[i].flag=false;
		p[i].solve=0;
		memset(p[i].score,-1,sizeof(p[i].score)); 	
	}
} 
int main(){
	scanf("%d%d%d",&n,&k,&m);
  	init();
	for(int i=1;i<=k;i++){
		scanf("%d",&full[i]);
	}
	int sid,tid,sc; //考生id,题目id ,得分
	for(int i=0;i<m;i++) {
		scanf("%d%d%d",&sid,&tid,&sc);
		if(sc!=-1){
			p[sid].flag=true;
		}
		if(sc==-1 && p[sid].score[tid]==-1 ){ //编译错 
			p[sid].score[tid]=0; 
		}   
		if(sc==full[tid] && p[sid].score[tid]<full[tid]){
			//p[sid].score[tid]=sc;
			p[sid].solve++;
		} 
		if(sc>p[sid].score[tid]){
			p[sid].score[tid]=sc;
		} 
		             
	}
	for(int i=1;i<=n;i++){	
		for(int j=1;j<=k;j++){
			if(p[i].score[j]!=-1){
				p[i].sum+=p[i].score[j];
			}
		}
	}
	sort(p+1,p+n+1,cmp);
	int r=1; //rank 
	for(int i=1;i<=n && p[i].flag ;i++){
		if(i>1 && p[i].sum!=p[i-1].sum){
			r=i;
		}
		printf("%d %05d %d",r,p[i].id,p[i].sum);
		for(int j=1;j<=k;j++){
			if(p[i].score[j]==-1){
				printf(" -");
			}else{
				printf(" %d",p[i].score[j]);
			}
		}
		printf("\n");
	}
	return 0;
}



1083 List Grades (25分)

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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
const int N=105; 
struct node{
	char name[11];
	char id[11];
	int grade;
}p[N]; 
bool cmp(node a,node b){
	return a.grade>b.grade;
}
int main(){
	int n,l,r;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%s%s%d",p[i].name,p[i].id,&p[i].grade);  // %s遇' '结束 
	}
	sort(p,p+n,cmp);
	scanf("%d%d",&l,&r);
	bool f=false;
	for(int i=0;i<n;i++){
		if(p[i].grade>=l && p[i].grade<=r){
			printf("%s %s\n",p[i].name,p[i].id);
			f=true;
		}
	}
	if(!f)printf("NONE\n");	
	return 0;
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值