PAT 乙級 1015德才論 (JAVA版)

1015 德才論 (25分)

宋代史學家司馬光在《資治通鑑》中有一段著名的“德才論”:“是故才德全盡謂之聖人,才德兼亡謂之愚人,德勝才謂之君子,才勝德謂之小人。凡取人之術,苟不得聖人,君子而與之,與其得小人,不若得愚人。”

現給出一批考生的德才分數,請根據司馬光的理論給出錄取排名。

輸入格式:

輸入第一行給出 3 個正整數,分別爲:N(≤10​5​​),即考生總數;L(≥60),爲錄取最低分數線,即德分和才分均不低於 L 的考生纔有資格被考慮錄取;H(<100),爲優先錄取線——德分和才分均不低於此線的被定義爲“才德全盡”,此類考生按德才總分從高到低排序;才分不到但德分到線的一類考生屬於“德勝才”,也按總分排序,但排在第一類考生之後;德才分均低於 H,但是德分不低於才分的考生屬於“才德兼亡”但尚有“德勝才”者,按總分排序,但排在第二類考生之後;其他達到最低線 L 的考生也按總分排序,但排在第三類考生之後。

隨後 N 行,每行給出一位考生的信息,包括:准考證號 德分 才分,其中准考證號爲 8 位整數,德才分爲區間 [0, 100] 內的整數。數字間以空格分隔。

輸出格式:

輸出第一行首先給出達到最低分數線的考生人數 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

 第一版:(部分正確、運行超時、答案錯誤)

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class Main {
static class Student{
	String studentID;
	int  moralPoint;
	int talendPoint;
	public int getMoralPoint() {
		return moralPoint;
	}
	public String getStudentID() {
		return studentID;
	}
	public int getTalendPoint() {
		return talendPoint;
	}
	public void setMoralPoint(int moralPoint) {
		this.moralPoint = moralPoint;
	}
	public void setTalendPoint(int talendPoint) {
		this.talendPoint = talendPoint;
	}
	public void setStudentID(String studentID) {
		this.studentID = studentID;
	}
}
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
	final int 	totalNumber=scanner.nextInt();
	final int buttomLevel=scanner.nextInt();
	final int 	topLevel=scanner.nextInt();
		ArrayList<Student> arrayList = new ArrayList<Student>();
		for(int i = 0; i<totalNumber;i++) {
			Student student = new Student();
			student.studentID = scanner.next();
			student.moralPoint = scanner.nextInt();
			student.talendPoint = scanner.nextInt();
		}
		List<Student> collect = arrayList.stream().filter(x ->(x.getMoralPoint()>=buttomLevel && x.getTalendPoint()>=buttomLevel)).collect(Collectors.toList());
		int total = collect.size();
		System.out.println(total);
		 List<Student> collect2 = arrayList.stream().filter(x ->(x.getMoralPoint()>=topLevel && x.getTalendPoint()>=topLevel)).collect(Collectors.toList());
		 collect2.stream().sorted(Comparator.comparing(student->(student.getMoralPoint()+student.getTalendPoint()))).forEach(System.out::println);
		 arrayList.removeAll(collect2);
		 List<Student> collect3 = arrayList.stream().filter(x ->(x.getMoralPoint()>=topLevel && x.getTalendPoint()<topLevel && x.getTalendPoint()>=buttomLevel )).collect(Collectors.toList());
		 collect3.stream().sorted(Comparator.comparing(student->(student.getMoralPoint()+student.getTalendPoint()))).forEach(System.out::println);
		 arrayList.removeAll(collect3);	
		 List<Student> collect4 = arrayList.stream().filter(x ->(x.getMoralPoint()>=buttomLevel && x.getTalendPoint()>=buttomLevel && x.getMoralPoint()>x.getTalendPoint())).collect(Collectors.toList());
		 collect4.stream().sorted(Comparator.comparing(student->(student.getMoralPoint()+student.getTalendPoint()))).forEach(System.out::println);
		 arrayList.removeAll(collect4);	
		 List<Student> collect5 = arrayList.stream().filter(x ->(x.getMoralPoint()>=buttomLevel && x.getTalendPoint()>=buttomLevel)).collect(Collectors.toList());
		 collect5.stream().sorted(Comparator.comparing(student->(student.getMoralPoint()+student.getTalendPoint()))).forEach(System.out::println);
	}
}

查找一下錯誤在哪裏:JAVA讓我絕望,找了好幾個,不是答案錯誤就是運行全部超時,果斷放棄找了C++

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct node {
    int num, de, cai;
};
int cmp(struct node a, struct node b) {
    if ((a.de + a.cai) != (b.de + b.cai))
        return (a.de + a.cai) > (b.de + b.cai);
    else if (a.de != b.de)
        return a.de > b.de;
    else
        return a.num < b.num;
}
int main() {
    int n, low, high;
    scanf("%d %d %d", &n, &low, &high);
    vector<node> v[4];
    node temp;
    int total = n;
    for (int i = 0; i < n; i++) {
        scanf("%d %d %d", &temp.num, &temp.de, &temp.cai);
        if (temp.de < low || temp.cai < low)
            total--;
        else if (temp.de >= high && temp.cai >= high)
            v[0].push_back(temp);
        else if (temp.de >= high && temp.cai < high)
            v[1].push_back(temp);
        else if (temp.de < high && temp.cai < high && temp.de >= temp.cai)
            v[2].push_back(temp);
        else
            v[3].push_back(temp);
    }
    printf("%d\n", total);
    for (int i = 0; i < 4; i++) {
        sort(v[i].begin(), v[i].end(), cmp);
        for (int j = 0; j < v[i].size(); j++)
            printf("%d %d %d\n", v[i][j].num, v[i][j].de, v[i][j].cai);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章