题目1023:EXCEL排序
参考代码:2 使用BuffRead 时间降下来了
参考代码:1 使用Scanner 差点超时
1023 | Accepted | ![]() | 159100KB | 2292B | 2970MS | Java / 代码 / 编辑 | 15:56:29 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main_Scanner {
private static class Student implements Comparable<Student> {
String id;
String name;
int mark;
int k;
public Student(String id, String name, int mark,int k) {
this.id = id;
this.name = name;
this.mark = mark;
this.k = k;
}
public String toString() {
return (this.id + " " +this.name+ " "+this.mark);
}
@Override
public int compareTo(Student s1) {
if(k==1) return (this.id.compareTo(s1.id));
else if(k==2){
if (this.name.equals(s1.name)==false)
return (this.name.compareTo(s1.name));
else
return (this.id.compareTo(s1.id));
}
else{
if (s1.mark != this.mark)
return -(s1.mark - this.mark);
else
return (this.id.compareTo(s1.id));
}
}
}
public static void main(String arg[]) throws IOException{
Scanner sc = new Scanner(System.in);
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Integer count=0;
while(sc.hasNext()){
Integer N = sc.nextInt();
Integer K = sc.nextInt();
if(N==0 && K==0 ) break;
String unuse = sc.nextLine();
List<Student> students = new ArrayList<Student>();
//学生列表加入
for(int i=0;i<N;i++){
String temp = sc.nextLine();
String[] str = temp.split(" ");
Integer mark = Integer.parseInt(str[2]);
students.add(new Student(str[0],str[1],mark,K));
}
Collections.sort(students);
count++;
System.out.println("Case "+count+":");
for(Student i :students){
System.out.println(i.toString());
}
students.clear();
}
}
}
参考代码:2 使用BuffRead 时间降下来了
1023 | Accepted | ![]() | 147408KB | 2421B | 2200MS | Java / 代码 / 编辑 | 16:06:10 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.BreakIterator;
import java.util.*;
public class Main {
private static class Student implements Comparable<Student> {
String id;
String name;
int mark;
int k;
public Student(String id, String name, int mark,int k) {
this.id = id;
this.name = name;
this.mark = mark;
this.k = k;
}
public String toString() {
return (this.id + " " +this.name+ " "+this.mark);
}
@Override
public int compareTo(Student s1) {
if(k==1) return (this.id.compareTo(s1.id));
else if(k==2){
if (this.name.equals(s1.name)==false)
return (this.name.compareTo(s1.name));
else
return (this.id.compareTo(s1.id));
}
else{
if (s1.mark != this.mark)
return -(s1.mark - this.mark);
else
return (this.id.compareTo(s1.id));
}
}
}
public static void main(String arg[]) throws IOException{
//Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Integer count=0;
while(true){
String temp1 = br.readLine();
String[] temp2 = temp1.split(" ");
Integer N = Integer.parseInt(temp2[0]);
Integer K = Integer.parseInt(temp2[1]);
if(N==0 && K==0 ) break;
//String unuse = sc.nextLine();
List<Student> students = new ArrayList<Student>();
//学生列表加入
for(int i=0;i<N;i++){
String temp = br.readLine();
String[] str = temp.split(" ");
Integer mark = Integer.parseInt(str[2]);
students.add(new Student(str[0],str[1],mark,K));
}
Collections.sort(students);
count++;
System.out.println("Case "+count+":");
for(Student i :students){
System.out.println(i.toString());
}
students.clear();
}
}
}
/**************************************************************
Problem: 1023
User: pucklina
Language: Java
Result: Accepted
Time:2200 ms
Memory:147408 kb
****************************************************************/