A1025 PAT Ranking (25分)

1025 PAT Ranking (25分)

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:
For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:
9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

分析

题意:
对于输入的id和分数按照 分数从高到低进行排序,每组内也同样进行排序,如果分数相同时按照注册id的字典序从小到大排序。

  • 通过先对考场内的进行排序,然后总体排序
  • 使用了cmp排序与struct

代码

java代码(只过了三个点)

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main{
	
	static class cmp implements Comparator<Student>{

		@Override
		public int compare(Student o1, Student o2) {
			// TODO Auto-generated method stub
			int a=-1;
			if(o1.score==o2.score){
				if(o1.id==o2.id.max(o1.id)){
				a=1;
				}
			}
			if(o1.score>o2.score){
				a= -1;
			}
			if(o1.score<o2.score){
				a= 1;
			} 
			return a;			
		}		
	}

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        Student  student[] = new Student[30000];
        int num=0;
        try{
        	int n = Integer.parseInt(br.readLine());
        	  
              for(int i=0;i<n;i++){
                  //num 为考生数
                  int k=Integer.parseInt(br.readLine());
                  for(int j=0;j<k;j++){
                      student[num]=new Student();
                      String arr= br.readLine();
                      String arrs[] = arr.split(" ");
                     
                      student[num].id=new BigInteger(arrs[0]);
                      student[num].score=Integer.parseInt(arrs[1]);
                      student[num].location_number=i+1;
                      num++;
                  }
                  //内部排序
                  int num1=num-k;
                 Arrays.sort(student, num1, num,new cmp());
                  //内部排名次
                  student[num-k].local_rank=1;
                  for(int j=num-k+1;j<num;j++){
                      if (student[j].score==student[j-1].score){
                          student[j].local_rank=student[j-1].local_rank;
                      }else {
                          student[j].local_rank=j+1-(num-k);
                      }
                  }
              }	
        }catch(IOException e){
        	e.printStackTrace();
        }
        //全部排序
        Arrays.sort(student,0,num,new cmp());
      out.println(num);
        int mm=1;
        for(int i=0;i<num;i++){
            if(i>0&&student[i].score==student[i-1].score){
                out.println(student[i].id+" "+mm+" "+student[i].location_number+" "+student[i].local_rank);
            }else {
                mm=i+1;
                out.println(student[i].id + " " + mm + " " + student[i].location_number + " " + student[i].local_rank);
               
            } out.flush();
        } 
    }
    public static class Student{
        BigInteger id;
        int score;
        int location_number;
        int local_rank;
        
    }

}

cpp代码

#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
struct Student{
	char id[15];
	int score;
	int local_rank;
	//int final_rank;
	int location_num;
}stu[300010];
bool cmp(Student a,Student b){
	if(a.score!=b.score)
	return a.score>b.score;
	else return strcmp(a.id,b.id)<0;//a的字典序小于b的字典序 
}
int main(){
	int n,num;
	scanf("%d",&n);
	for (int i=0;i<n;i++){
		int k;
		scanf("%d",&k);
		for(int j=0;j<k;j++){
			scanf("%s%d",stu[num].id,&stu[num].score);
			stu[num].location_num=i+1;
			num++;
		}
		sort(stu+num-k,stu+num,cmp);
		stu[num-k].local_rank = 1;
		for (int l=num-k+1;l<num;l++){
			if(stu[l].score==stu[l-1].score)
			stu[l].local_rank = stu[l-1].local_rank;
			else stu[l].local_rank = k-(num -l)+1;
			/* 考场内的排名等于 考场内的序号  
			原先是 (num-k,num) 共k个人,当序号为l时,n内排为 k-(num-l)+1 
			*/
		} 		
	}
		printf("%d\n",num);
		sort(stu,stu + num,cmp);
		int r=1;
		for (int i=0;i<num;i++){
			if(i>0&&stu[i].score==stu[i-1].score)
				printf("%s %d %d %d\n",stu[i].id,r,stu[i].location_num,stu[i].local_rank);			
			else {
				printf("%s %d %d %d\n",stu[i].id,(i+1),stu[i].location_num,stu[i].local_rank);
				r = i+1;
			}		
		} 
	return 0;
} 

总结

  • 结构体struct的使用
  • cmp算法的使用
#include <algorithm>
using namespace std;
bool cmp(int a,int b){
	return a>b;//a大于b表示 a排在前面
	//strcmp的使用,如果这里a,b为字符串的时候
	strcmp(a,b) <0 表示 字典序小的排在前面
}
//使用的时候,传入的参数可以直接在stu上进行加减表示stu的范围。 
int m;
sort(stu+m,stu-m,cmp);

参考

《算法笔记》· 胡凡

发布了91 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/WeDon_t/article/details/104099834