首页 > 试题广场 >

德才论 (25)

[编程题]德才论 (25)
  • 热度指数:38274 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之
小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

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

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

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


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

输入

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.Scanner;

public class Main {
    public Main() {
        
    }
    public Main(int sno, byte de_grade, byte cai_grade) {
        super();
        this.sno = sno;
        this.de_grade = de_grade;
        this.cai_grade = cai_grade;
    }
    int sno;//准考证号
    byte de_grade;//德分
    byte cai_grade;//才分
    int all_grade;//总分
    String des;//对于分数描述,比如:德才尽全
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();//学生人数
        byte L = sc.nextByte();//德才录取线
        byte H = sc.nextByte();//优先录取线
        ArrayList<Main> main = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            main.add(new Main(sc.nextInt(),sc.nextByte(),sc.nextByte()));
        }
        ArrayList<Main> arr1 = new ArrayList<>();
        ArrayList<Main> arr2 = new ArrayList<>();
        ArrayList<Main> arr3 = new ArrayList<>();
        ArrayList<Main> arr4 = new ArrayList<>();
        for (int i = 0; i < main.size(); i++) {
            Main temp = main.get(i);
            temp.all_grade = temp.de_grade+temp.cai_grade;
            if(temp.de_grade>=L&&temp.cai_grade>=L) {
                if(temp.de_grade>=H&&temp.cai_grade>=H) {
                    arr1.add(temp);
                }
                else if(temp.de_grade>=H&&temp.cai_grade<H) {
                    arr2.add(temp);
                }    
                else if(temp.de_grade<H&&temp.cai_grade<H&&
                        temp.de_grade>=temp.cai_grade) {
                    arr3.add(temp);
                }else {
                    arr4.add(temp);
                }
            }
        }
        sort(arr2);
        sort(arr3);
        sort(arr4);
        sort(arr1);
        //main.sort();
        System.out.println(arr1.size()+arr2.size()
        +arr3.size()+arr4.size());
        print(arr1);
        print(arr2);
        print(arr3);
        print(arr4);
    }
    
    public static void sort(ArrayList<Main> arr) {
        for (int i = 0; i < arr.size()-1; i++) {
            for(int j = i+1;j < arr.size();j++) {
                
                if(arr.get(i).all_grade<arr.get(j).all_grade) {
                    swap(arr,i,j);
                }else if(arr.get(i).all_grade==arr.get(j).all_grade){
                    if(arr.get(i).de_grade<arr.get(j).de_grade) {
                        swap(arr,i,j);
                    }
                }
            }
        }
    }
    public static void swap(ArrayList<Main> arr,int i,int j) {
        Main temp = arr.get(i);
        arr.set(i,arr.get(j));
        arr.set(j, temp);
    }
    public static void print(ArrayList<Main> arr) {
        for (int i = 0; i < arr.size(); i++) {
            System.out.println(arr.get(i).toString());
        }
    }
    @Override
    public String toString() {
        return sno + " " + de_grade + " " + cai_grade;
    }
}

不知道为什么有些案例通过不了,求解答
发表于 2023-03-02 11:29:37 回复(0)
import java.util.*;
import java.text.*;

class Main{
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        int N =sc.nextInt();
        int L = sc.nextInt();
        int H = sc.nextInt();
        int m = 0;
        List<Person> l1 = new ArrayList<>();
        List<Person> l2 = new ArrayList<>();
        List<Person> l3 = new ArrayList<>();
        List<Person> l4 = new ArrayList<>();
        Person [] p =new Person[N];

        for (int i=0;i<N;i++){
            p[i]=new Person(sc.next(),sc.nextInt(),sc.nextInt());

            if(p[i].D>=L&&p[i].C>=L){
                m++;
                if (p[i].D>=H&&p[i].C>=H){
                    l1.add(p[i]);
                }else if (p[i].D>=H&&p[i].C<H){
                    l2.add(p[i]);
                }else if (p[i].D<H&&p[i].C<H&&p[i].D>=p[i].C){
                    l3.add(p[i]);
                }else {
                    l4.add(p[i]);
                }
            }
        }

        l1.sort(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                if (o1.D + o1.C > o2.D + o2.C) {
                    return -1;
                } else if ((o1.D + o1.C == o2.D + o2.C) && (o2.D - o1.D != 0)) {
                    return o2.D - o1.D;
                } else if ((o1.D + o1.C == o2.D + o2.C) && (o2.D - o1.D == 0)) {
                    return o1.num.compareTo(o2.num);
                } else {
                    return 1;
                }
            }
        });

        l2.sort(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                if (o1.D + o1.C > o2.D + o2.C) {
                    return -1;
                } else if ((o1.D + o1.C == o2.D + o2.C) && (o2.D - o1.D != 0)) {
                    return o2.D - o1.D;
                } else if ((o1.D + o1.C == o2.D + o2.C) && (o2.D - o1.D == 0)) {
                    return o1.num.compareTo(o2.num);
                } else {
                    return 1;
                }
            }
        });

        l3.sort(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                if (o1.D + o1.C > o2.D + o2.C) {
                    return -1;
                } else if ((o1.D + o1.C == o2.D + o2.C) && (o2.D - o1.D != 0)) {
                    return o2.D - o1.D;
                } else if ((o1.D + o1.C == o2.D + o2.C) && (o2.D - o1.D == 0)) {
                    return o1.num.compareTo(o2.num);
                } else {
                    return 1;
                }
            }
        });

        l4.sort(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                if (o1.D + o1.C > o2.D + o2.C) {
                    return -1;
                } else if ((o1.D + o1.C == o2.D + o2.C) && (o2.D - o1.D != 0)) {
                    return o2.D - o1.D;
                } else if ((o1.D + o1.C == o2.D + o2.C) && (o2.D - o1.D == 0)) {
                    return o1.num.compareTo(o2.num);
                } else {
                    return 1;
                }
            }
        });

        System.out.println(m);
        for (Person x:l1) {
            x.getMessage();
        }

        for (Person x:l2) {
            x.getMessage();
        }

        for (Person x:l3) {
            x.getMessage();
        }

        for (Person x:l4) {
            x.getMessage();
        }
    }
}

class Person{
    String num;
    int D;
    int C;
    public Person(String num,int D,int C){
        this.C = C;
        this.D = D;
        this.num = num;
    }
    public void getMessage(){
        System.out.println(this.num+" "+this.D+" "+this.C);
    }
}
是不是pat上java只能超时啊 无语了 

发表于 2022-03-08 16:03:49 回复(0)

import java.util.*;

public class T1005 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		Student.low = sc.nextInt();
		Student.high = sc.nextInt();
		List<Student> list = new ArrayList<>();
		for (int i = 0; i < N; i++) {
			String id = sc.next();
			int virtue = sc.nextInt();
			int talent = sc.nextInt();
			Student stu = new Student(id, virtue, talent);
			if (stu.getType() != Student.NO_TALENT_NO_TALENT) {
				list.add(stu);
			}
		}
		sc.close();
		System.out.println(list.size());
		Collections.sort(list);
		for (Student stu : list) {
			System.out.println(stu);
		}
	}
}

class Student implements Comparable<Student> {
	public static final int FULL_TALENT_FULL_VIRTUE = 1;// 才德全尽-圣人
	public static final int FULL_VIRTUE_LESS_TALENT = 2;// 德胜才-君子
	public static final int NONE_BUT_MORE_VIRTUE = 3;// 才德兼亡-愚人(德胜才)
	public static final int MORE_TALENT = 4;// 才胜德-小人,才德兼亡-愚人(才胜德)
	public static final int NO_TALENT_NO_TALENT = 5;// 落选
	public static int low = 60;
	public static int high = 90;
	private String id;
	private int virtue;
	private int talent;
	private int type;

	public Student(String id, int virtue, int talent) {
		this.id = id;
		this.virtue = virtue;
		this.talent = talent;
		this.classify();
	}

	private void classify() {
		if (virtue < low || talent < low) {
			type = Student.NO_TALENT_NO_TALENT;
		} else if (virtue >= high && talent >= high) {
			type = Student.FULL_TALENT_FULL_VIRTUE;
		} else if (virtue >= high) {
			type = Student.FULL_VIRTUE_LESS_TALENT;
		} else if (virtue >= talent) {
			type = Student.NONE_BUT_MORE_VIRTUE;
		} else {
			type = Student.MORE_TALENT;
		}
	}

	public int compareTo(Student o) {
		if (this.type != o.type) {// 类型升序
			return this.type - o.type;
		}
		if ((this.virtue + this.talent) != (o.virtue + o.talent)) {// 总分降序
			return (o.virtue + o.talent) - (this.virtue + this.talent);
		}
		if (this.virtue != o.virtue) {// 德分降序
			return o.virtue - this.virtue;
		}
		return this.id.compareTo(o.id);// 准考证号升序
	}

	public int getType() {
		return this.type;
	}

	public String toString() {
		return id + " " + virtue + " " + talent;
	}
}

发表于 2019-09-04 22:18:56 回复(0)

牛客这个判断是不是有问题啊,我看了我的都超时间也超内存了,还是给我显示通过了。

时间限制 1000 ms 内存限制 32768 KB

1    答案正确    1437    38460
2    答案正确    1405    42980
3    答案正确    1361    41632
4    答案正确    1508    38212
5    答案正确    1471    38940
6    答案正确    1371    37796
7    答案正确    1226    39852

贴个代码

import java.util.*;
public class Pat1005 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int L = in.nextInt();
        int H = in.nextInt();
        Set result = new TreeSet();
        while(N > 0) {
            int id = in.nextInt();
            int morality = in.nextInt();
            int knowledge = in.nextInt();
            int level = 0;
            N--;
            if(morality < L || knowledge < L)
                continue;

            if(morality >= H && knowledge >= H)
                level = 1;
            else if(morality >= H && knowledge < H)
                level = 2;
            else if(morality= knowledge)
                level = 3;
            else
                level = 4;
            result.add(new Student(id, morality, knowledge, level));
        }
        System.out.println(result.size());
        for(Student s : result)
            System.out.println(s);
    }
    public static class Student implements Comparable {
        int id = 0;
        int morality = 0;
        int knowledge = 0;
        int level = 0;
        int total = 0;
        public Student(int id, int morality, int knowledge, int level) {
            this.id = id;
            this.morality = morality;
            this.knowledge = knowledge;
            this.level = level;
            this.total = morality + knowledge;
        }
        public String toString() {
            return id + " " + morality + " " + knowledge;
        }
        @Override
        public int compareTo(Student s) {
            if(level > s.level)
                return 1;
            if(level < s.level)
                return -1;
            if(total > s.total)
                return -1;
            if(total < s.total)
                return 1;

            if(total == s.total) {
                if(morality > s.morality)
                    return -1;
                else if(morality == s.morality)
                    return id < s.id ? -1 : 1;
                else
                    return 1;
            }
            return 1;
        }
    }
}
发表于 2019-06-12 23:03:22 回复(0)
/* 使用冒泡排序全部超时
 * 改成使用归并排序
 * 
 * 基本思路 
 *    1.先分类,同时统计总分,按照分类放大总分。
 *    2.再排序。 
 */

import java.util.Scanner;

public class Main
{
    // 归并排序
    public static void sort(int[][] arr, int left, int right)
    {
        int mid = (left + right) / 2;
        if (left < right) {
            sort(arr, left, mid);
            sort(arr, mid+1, right);
            // 合并
            merge(arr, left, mid, right);
        }
    }

    public static void merge(int[][] arr, int left, int mid, int right)
    {
        int[][] tmp = new int[right-left+1][];
        int l = left;
        int m = mid + 1;
        int i = 0;

        for(; l <= mid && m <= right; i++)
        {
            if (arr[l][3] < arr[m][3])
                tmp[i] = arr[m++];
            else if (arr[l][3] == arr[m][3]) {
                // 总分相等, 比较德分
                if (arr[l][1] > arr[m][1])
                    tmp[i] = arr[l++];
                else if (arr[l][1] < arr[m][1])
                    tmp[i] = arr[m++];
                else {
                    // 德分相等,比较考生号
                    if (arr[l][0] < arr[m][0])
                        tmp[i] = arr[l++];
                    else
                        tmp[i] = arr[m++];
                }
            } else {
                tmp[i] = arr[l++];
            }
        }

        while(l <= mid)
            tmp[i++] = arr[l++];

        while(m <= right)
            tmp[i++] = arr[m++];

        // temp数组中的元素传入待排序数组中
        for (int j = 0; j < i; j++) {
            arr[left + j] = tmp[j];
        }
    }

    public static void main(String[] args) {
        // input
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        int low = sc.nextInt();
        int height = sc.nextInt();
        int[][] arr = new int[count][4];
        int a, d, c, t;
        for (int i = 0; i < count; i++)
        {
            a = sc.nextInt();
            d = sc.nextInt();
            c = sc.nextInt();
            t = d + c;
            if (d < low || c < low) {
                // 不及格
                count--;
                i--;
                continue;
            }
            if (d >= height) {
                if (c >= height) {
                    // 德才兼备
                   t += 3 * height;
                } else {
                    // 德胜才
                    t += 2 * height;
                }
            } else if (c < height && d >= c) {
                // “才德兼亡”但尚有“德胜才”
                t += height;
            }
            arr[i][0] = a;
            arr[i][1] = d;
            arr[i][2] = c;
            arr[i][3] = t;
        }
        
        // sorting
        // 冒泡排序 - 超时
        // 归并排序
        sort(arr, 0, count);

        // output
        System.out.println(count);
        for (int i = 0; i < count; i++) {
            System.out.println(
                    arr[i][0] + " "
                    + arr[i][1] + " "
                    + arr[i][2]
            );
        }
    }
}

发表于 2018-11-15 17:17:25 回复(0)
importjava.util.ArrayList;
importjava.util.Comparator;
importjava.util.Scanner;
 
publicclassMain {
    publicstaticvoidmain(String[] args) {
        solveVirtueAndTalent();
    }
 
    privatestaticvoidsolveVirtueAndTalent() {
        Scanner scanner = newScanner(System.in);
        intnum = scanner.nextInt();//人数
        intL = scanner.nextInt();//达标分
        intH = scanner.nextInt();//优秀分
        ArrayList<Grade> class1 = newArrayList<>();
        ArrayList<Grade> class2 = newArrayList<>();
        ArrayList<Grade> class3 = newArrayList<>();
        ArrayList<Grade> class4 = newArrayList<>();
        //达到最低分数线的考生人数M
        intM = 0;
        for(inti = 0; i < num; i++) {
            Grade grade1 = newGrade(scanner.nextInt(), scanner.nextInt(), scanner.nextInt());
            if(grade1.gradeVirtue < L || grade1.gradeTalent < L) {
                continue;
            }
            M++;
            if(grade1.gradeVirtue >= H && grade1.gradeTalent >= H) {
                class1.add(grade1);
            } elseif(grade1.gradeVirtue >= H) {// && grade1.gradeVirtue > grade1.gradeTalent
                class2.add(grade1);
            } elseif(grade1.gradeVirtue >= grade1.gradeTalent) {
                class3.add(grade1);
            } else{
                class4.add(grade1);
            }
        }
        Comparator<Grade> comparator = newComparator<Grade>() {
            @Override
            publicintcompare(Grade o1, Grade o2) {
                if(o1.sumGrade() != o2.sumGrade()) {
                    returno1.sumGrade() - o2.sumGrade();
                }
                if(o1.gradeVirtue != o2.gradeVirtue) {
                    returno1.gradeVirtue - o2.gradeVirtue;
                }
                if(o1.examNum != o2.examNum) {//降序排列
                    return-(o1.examNum - o2.examNum);
                }
                return0;
            }
        };
        class1.sort(comparator);
        class2.sort(comparator);
        class3.sort(comparator);
        class4.sort(comparator);
        System.out.println(M);
        printArrayList(class1);
        printArrayList(class2);
        printArrayList(class3);
        printArrayList(class4);
    }
 
    privatestaticvoidprintArrayList(ArrayList<Grade> class1) {
        for(inti = class1.size() - 1; i >= 0; i--) {
            Grade grade = class1.get(i);
            System.out.println(grade);
        }
    }
 
    //德才论解题思路
    //14 60 80
    //有资格,不低于60;
    // 一类:才德全尽,不低于80
    // 二类:德胜才,德分到线80
    // 三类:“才德兼亡”但尚有“德胜才”
    // 四类:有资格,按总分排序
    staticclassGrade {
        intexamNum;//考号
        intgradeVirtue;//德
        intgradeTalent;//才
 
        publicGrade(intexamNum, intgradeVirtue, intgradeTalent) {
            this.examNum = examNum;
            this.gradeVirtue = gradeVirtue;
            this.gradeTalent = gradeTalent;
        }
 
        publicintsumGrade() {
            returngradeVirtue + gradeTalent;
        }
 
        @Override
        publicString toString() {
            returnexamNum + " "+ gradeVirtue + " "+ gradeTalent;
//            return super.toString();
        }
    }
}

发表于 2018-06-05 16:46:52 回复(0)
关键在于学生排序的算法compareTo()

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.TreeSet;
public class Main {
    private static class Student implements Comparable<Student>{
        publicStudent(intid, intmorality, intability) {
            // TODO Auto-generated constructor stub
            this.id = id;
            this.morality = morality;
            this.ability = ability;
        }
        int id = 0;
        int morality = 0;
        int ability = 0;
 
        @Override
        public int compareTo(Student o) {
            // TODO Auto-generated method stub
             
            intresult = 0;
 
            if(o instanceof Student) {
                Student student = (Student)o;
                 
                if((this.ability + this.morality) >
                    (student.ability + student.morality)) {
                    result = -1;
                }elseif((this.ability + this.morality) ==
                (student.ability + student.morality)) {
                    if(this.morality > student.morality) {
                        result = -1;
                    }elseif(this.morality == student.morality) {
                        if(this.id < student.id) {
                            result = -1;
                        }elseif(this.id == student.id) {
                            result = 0;
                        }else{
                            result = 1;
                        }
                    }else{
                        result = 1;
                    }
                }else{
                    result = 1;
                }
            }
            return result;
        }
         
        public void print Student() {
            System.out.println(this.id + " "+
                    this.morality + " "+ this.ability);
        }
    }
 
     
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
         
        String line = bufferedReader.readLine();
        String[] params = line.split(" ");
         
        intN = 0;
        intL = 0;
        intH = 0;
         
        N = Integer.parseInt(params[0]);
        L = Integer.parseInt(params[1]);
        H = Integer.parseInt(params[2]);
         
        Student[] students = newStudent[N];
         
        for(int i = 0; i < N; i++) {
            line = bufferedReader.readLine();
            params = line.split(" ");
            students[i] = newStudent(Integer.parseInt(params[0]), Integer.parseInt(params[1]),Integer.parseInt(params[2]));
        }
         
        TreeSet[] studentClass = new TreeSet[4];
         
        studentClass[0] = new TreeSet<Student>();
        studentClass[1] = new TreeSet<Student>();
        studentClass[2] = new TreeSet<Student>();
        studentClass[3] = new TreeSet<Student>();
         
        int passStudentCount = 0;
         
        for(int i = 0; i < students.length; i++) {
            if(students[i].ability >= H && students[i].morality >= H) {
                studentClass[0].add(students[i]);
                passStudentCount++;
            }elseif(students[i].ability >= L && students[i].ability < H &&
                    students[i].morality >= H) {
                studentClass[1].add(students[i]);
                passStudentCount++;
            }elseif(students[i].ability >= L && students[i].ability < H &&
                    students[i].morality >= L && students[i].morality < H &&
                    students[i].morality >= students[i].ability) {
                studentClass[2].add(students[i]);
                passStudentCount++;
            }elseif(students[i].ability >= L && students[i].morality >= L) {
                studentClass[3].add(students[i]);
                passStudentCount++;
            }
        }
         
        System.out.println(passStudentCount);
         
        Iterator<Student> treeSetIterator = null;
         
        for(int i = 0; i < studentClass.length; i++) {
            treeSetIterator = studentClass[i].iterator();
            while(treeSetIterator.hasNext()) {
                Student tempStudent = treeSetIterator.next();
                tempStudent.printStudent();
            }
        }
    }
}

编辑于 2017-11-15 11:58:46 回复(0)

思路:
首先采用类将student的数据封装,包括序号、学号、成绩,然后在循环判断的时候,排序的算法将学生序号放入对应数组,然后采用插入排序的算法进行排序。

import java.util.Scanner;
public class moralityAndAbility {
public static void main(String[] args) {
int total,min,pre;
Scanner in = new Scanner(System.in);
total = in.nextInt();
min = in.nextInt();
pre = in.nextInt();
int first = 0,second = 0,third = 0,forth = 0;

student[] stu = new student[100000];
    for(int i = 0;i < total;i++){
        stu[i] = new student();
    }
    for (int i = 0;i < total;i++){
        stu[i].id = i;
        stu[i].no = in.nextInt();
        stu[i].grade = in.nextInt();
        stu[i].score = in.nextInt();
        stu[i].total = stu[i].grade + stu[i].score;
    }

    int[] firstGroup = new int[100000];
    int[] secondGroup = new int[100000];
    int[] thirdGroup = new int[100000];
    int[] forthGroup = new int[100000];
    for (int i = 0;i < total;i++){
        if (stu[i].grade < min || stu[i].score < min) continue;
        if (stu[i].grade >= pre && stu[i].score >= pre){
            firstGroup[first++] = stu[i].id;
            insertSort(firstGroup,first-1,stu);
        }else if (stu[i].grade >= pre && stu[i].score >= min){
            secondGroup[second++] = stu[i].id;
            insertSort(secondGroup,second-1,stu);
        }else if (stu[i].grade >= stu[i].score && stu[i].score >= min){
            thirdGroup[third++] = stu[i].id;
            insertSort(thirdGroup,third-1,stu);
        }else if (stu[i].grade >= min && stu[i].score >= min){
            forthGroup[forth++] = stu[i].id;
            insertSort(forthGroup,forth-1,stu);
        }
    }

    System.out.println(first+second+third+forth);
    for (int i = 0;i < first;i++){
        System.out.println(stu[firstGroup[i]].no + " " + stu[firstGroup[i]].grade + " " + stu[firstGroup[i]].score);
    }
    for (int i = 0;i < second;i++){
        System.out.println(stu[secondGroup[i]].no + " " + stu[secondGroup[i]].grade + " " + stu[secondGroup[i]].score);
    }
    for (int i = 0;i < third;i++){
        System.out.println(stu[thirdGroup[i]].no + " " + stu[thirdGroup[i]].grade + " " + stu[thirdGroup[i]].score);
    }
    for (int i = 0;i < forth;i++){
        System.out.println(stu[forthGroup[i]].no + " " + stu[forthGroup[i]].grade + " " + stu[forthGroup[i]].score);
    }

}

private static void insertSort(int[] a,int b,student[] stu){
    for (int i = b;i > 0;i--){
        if (stu[a[i]].total > stu[a[i-1]].total  || (stu[a[i]].total == stu[a[i-1]].total && stu[a[i]].grade > stu[a[i-1]].grade)){
            int temp = a[i];
            a[i] = a[i-1];
            a[i-1] = temp;
        } else break;
    }
}

private static class student{
    student(){}
    int id;//记录是第几个
    int no;//考号
    int grade,score;//德分,才分
    int total;
}

}

发表于 2017-08-27 22:31:50 回复(1)
import java.util.*;
public class Main{
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
	    int N=sc.nextInt();
	    int L=sc.nextInt();
	    int H=sc.nextInt();
	    ArrayList<Student> al=new ArrayList<Student>();
	    for(int i=0;i<N;i++){
	        int id=sc.nextInt();
	        int d=sc.nextInt();
	        int c=sc.nextInt();
	        if(d>=L&&c>=L){
	            al.add(new Student(id,d,c,H));
	        }
	    }
	    Collections.sort(al,new Comparator<Student>(){
	        public int compare(Student s1,Student s2){
	            int res1=s1.level-s2.level;
	            int res2=res1==0?s2.sum-s1.sum:res1;
	            int res3=res2==0?(s2.d-s1.d):res2;
	            int res4=res3==0?(s1.id-s2.id):res3;
	            return res4;
	        }
	    });
	    System.out.println(al.size());
	    for(Student s:al){
	        System.out.println(s.id+" "+s.d+" "+s.c);
	    }
	}
    
}
class Student{
     int id;
     int d;
     int c;
     int sum;
     int level;
    public Student(int id,int d,int c,int H){
        this.id=id;
        this.d=d;
        this.c=c;
        this.sum=d+c;
        if(d>=H&&c>=H){
            this.level=1;
        }else if(d>=H){
            this.level=2;
        }else if(d<=H&&c<=H&&d>=c){
            this.level=3;
        }else{
            this.level=4;
        }
    }
}

发表于 2017-07-13 15:15:35 回复(1)
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N  = in.nextInt();
        int L = in.nextInt();
        int H = in.nextInt();
        TreeSet<Student> students = new TreeSet<Student>();
        for (int i = 0; i < N; i ++) {
            Student student = new Student(in.nextInt(), in.nextInt(), in.nextInt(), H);
            if (student.de >= L && student.cai >= L)
                students.add(student);
        }
        System.out.println(students.size());
        for (Student student : students)
            System.out.println(student.id + " " + student.de + " " + student.cai);


    }
    static class Student implements Comparable<Student>{
        int id, de, cai, level;
        Student(int id, int de, int cai, int H) {
            this.id = id;
            this.de = de;
            this.cai = cai;
            if (this.de >= H && this.cai >= H)
                level = 4;
            else if (this.de >= H)
                level = 3;
            else if (this.de >= this.cai)
                level = 2;
            else
                level = 1;
        }

        @Override
        public int compareTo(Student o) {
            int sum1 = this.de + this.cai;
            int sum2 = o.de + o.cai;
            if (this.level > o.level)
                return -1;
            else if(this.level == o.level) {
                if (sum1 > sum2)
                    return -1;
                else if(sum1 == sum2) {
                    if (this.de > o.de)
                        return -1;
                    else if (this.de == o.de && this.id < o.id)
                        return -1;
                }
            }
            return 1;
        }
    }
}

为什么总是提示内存超限,感觉没什么问题,求解答?

发表于 2017-07-06 00:17:31 回复(1)