pat 乙级 1015. 德才论 (25) c++




http://39.106.25.239

个人网站 欢迎访问 交流

1015. 德才论 (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Li

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:

输入第1行给出3个正整数,分别为:N(<=105),即考生总数;L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线L的考生也按总分排序,但排在第三类考生之后。

随后N行,每行给出一位考生的信息,包括:准考证号、德分、才分,其中准考证号为8位整数,德才分为区间[0, 100]内的整数。数字间以空格分隔。

输出格式:

输出第1行首先给出达到最低分数线的考生人数M,随后M行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:
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
输出样例:
 
  

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


提交代码

思路分析:

本题的考点就是compare函数排序。 最重要的就是对数据对象进行排序,关键的核心也在于如何写排序条件,实际上在你掌握了compare方法的原理之后,再声明一个结构体数组这个题就迎刃而解了。

(我就是不太熟悉c++的开始用的java写的但就是有三个测试点不能a,在网上找到别人的java代码后也是那三个点不能a,(虽然在牛客网的系统上可以a),经过改造后的代码在c++里实现果然就通过了,但还是要注意在多次输出的时候,最好采用printf()的方式 ,某些情况下会有超时的情况(这个题就是)!!!)。

思想很简单 ,看代码实现吧

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;


struct Student{
 	
	   int xue;
	   int de;
	   int cai;
	   int zong;
	
};
bool compare(Student o1,Student o2){
	if(o1.zong != o2.zong){
					return o1.zong > o2.zong;
				}else if(o1.de != o2.de ){
					return o1.de > o2.de;
				}else{
					return o2.xue > o1.xue;
				} 
				
}
int main(){
	int N,L,H;
	scanf("%d%d%d",&N,&L,&H);
	vector<Student> a1;
	vector<Student> a2;
	vector<Student> a3;
	vector<Student> a4;
	for(int i=0;i<N;i++){
		int xue,de,cai;
		cin>>xue>>de>>cai;
		if(de>=L && cai>=L){
			Student stu;
				stu.xue=xue;
				stu.de = de;
				stu.cai = cai;
				stu.zong = cai+de;
			
				if(de >= H && cai>= H){
					a1.push_back(stu);
				}else if(cai <H && de>=H){
					a2.push_back(stu);
				}else if(cai <H && de <H && de>=cai ){
					a3.push_back(stu);
				}else {
					a4.push_back(stu);
				}
			
			}
		
	}
	sort(a1.begin(),a1.end(),compare);
	sort(a2.begin(),a2.end(),compare);
	sort(a3.begin(),a3.end(),compare);
	sort(a4.begin(),a4.end(),compare);
		cout<<a1.size()+a2.size()+a3.size()+a4.size()<<endl;
		for(int i=0;i<a1.size();i++){
			printf("%d %d %d\n",a1[i].xue,a1[i].de,a1[i].cai);
	
		}for(int i=0;i<a2.size();i++){
		printf("%d %d %d\n",a2[i].xue,a2[i].de,a2[i].cai);
		}
		for(int i=0;i<a3.size();i++){
		printf("%d %d %d\n",a3[i].xue,a3[i].de,a3[i].cai);
		}
		for(int i=0;i<a4.size();i++){
		printf("%d %d %d\n",a4[i].xue,a4[i].de,a4[i].cai);
		}
	return 0;
}

java代码(19分):
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
 class Student{
 public int xue;
  public int de;
 public int cai;
 public int zong;
 public Student(int xue,int de, int cai, int zong) {
  super();
  this.xue = xue;
  this.de = de;
  this.cai = cai;
  this.zong = zong;
 }
 public void toSTring(){
  System.out.println(xue+" "+de +" "+cai );
 }
 
}
public class Main{
 static Scanner scan = new Scanner(new BufferedInputStream(System.in));
 public static void main(String[] args) {
  ArrayList<Student> a1=new ArrayList<Student>();
  ArrayList<Student> a2=new ArrayList<Student>();
  ArrayList<Student> a3=new ArrayList<Student>();
  ArrayList<Student> a4=new ArrayList<Student>();
  int N = scan.nextInt();
  int L =scan.nextInt();
  int H =scan.nextInt();
  Comparator<Student > c =new Comparator<Student>() {

   @Override
   public int compare(Student o1, Student o2){
    if(o1.zong != o2.zong){
     return o2.zong - o1.zong;
    }else if(o1.de != o2.de ){
     return o2.de - o1.de;
    }else return o1.xue - o2.xue;
   }
  };
  for(int i=0;i<N;i++){
   int xue,de,cai;
   xue = scan .nextInt();
   de = scan.nextInt();
   cai = scan.nextInt();
   if(de>=L && cai>=L){
    if(de >= H && cai>= H){
     a1.add(new Student(xue,de, cai, de+cai));
    }else if(cai <H && de>=H){
     a2.add(new Student(xue,de, cai, de+cai));
    }else if(cai <H && de <H && de>=cai ){
     a3.add(new Student(xue,de , cai , de+cai));
    }else {
     a4.add(new Student(xue,de , cai , de + cai));
    }
   }
   
  }
  
  Collections.sort(a1,c);
  Collections.sort(a2,c);
  Collections.sort(a3,c);
  Collections.sort(a4,c);
  System.out.println(a1.size()+a2.size()+a3.size()+a4.size());
  for(int i=0;i<a1.size();i++){
   a1.get(i).toSTring();
  }for(int i=0;i<a2.size();i++){
   a2.get(i).toSTring();
  }
  for(int i=0;i<a3.size();i++){
   a3.get(i).toSTring();
  }
  for(int i=0;i<a4.size();i++){
   a4.get(i).toSTring();
  }
  
  
 }

}




猜你喜欢

转载自blog.csdn.net/u010968350/article/details/78736336