题目4 EXCEL排序

 

 

 1 import java.util.Arrays;
 2 import java.util.Comparator;
 3 import java.util.Scanner;
 4  
 5 public class Main{
 6     private static class stu{
 7         String num;
 8         String name;
 9         int score;
10         @Override
11         public String toString() {
12             return num+" "+name+" "+score;
13         }
14     }
15     private static class C1 implements Comparator<stu>{
16         @Override
17         public int compare(stu o1, stu o2) {
18             return o1.num.compareTo(o2.num);
19         }
20     }
21     private static class C2 implements Comparator<stu>{
22         @Override
23         public int compare(stu o1, stu o2) {
24             if(o1.name.compareTo(o2.name)!=0)
25                 return o1.name.compareTo(o2.name);
26             else
27                 return o1.num.compareTo(o2.num);
28         }
29     }
30     private static class C3 implements Comparator<stu>{
31         @Override
32         public int compare(stu o1, stu o2) {
33             if(o1.score>o2.score) return 1;
34             else if(o1.score<o2.score) return -1;
35             else return o1.num.compareTo(o2.num);
36         }
37     }
38      
39     public static void main(String[]args){
40         stu[] student=new stu[100000];
41         for(int i=0;i<100000;i++)
42             student[i]=new stu();
43         Scanner in=new Scanner(System.in);
44         int count=0;
45         while(in.hasNext()){
46             int N=in.nextInt();
47             int C=in.nextInt();
48             if(N==0) break;
49             for(int i=0;i<N;i++){
50                 student[i].num=in.next();
51                 student[i].name=in.next();
52                 student[i].score=in.nextInt();
53             }
54             switch (C) {
55             case 1:
56                 C1 c1=new C1();
57                 Arrays.sort(student,0,N,c1);//0表示第一个下标,N表示最后一个下标+1
58                 break;
59             case 2:
60                 C2 c2=new C2();
61                 Arrays.sort(student,0,N,c2);
62                 break;
63             case 3:
64                 C3 c3=new C3();
65                 Arrays.sort(student,0,N,c3);
66                 break;
67             default:
68                 break;
69             }
70             count++;
71             System.out.println("Case "+count+":");
72             for(int i=0;i<N;i++)
73                 System.out.println(student[i]);
74         }
75     }
76 }
77 /**************************************************************
78     Problem: 1023
79     User: 0000H
80     Language: Java
81     Result: Time Limit Exceed
82 ****************************************************************/

 

posted @ 2015-09-09 12:24  打小孩  阅读(162)  评论(0编辑  收藏  举报