首页 > 试题广场 >

List Sorting (25)

[编程题]List Sorting (25)
  • 热度指数:4079 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
Excel can sort records according to any column. Now you are supposed to imitate this function.

输入描述:
Each input file contains one test case.  For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with.  Then N lines follow, each contains a record of a student.  A student's record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).


输出描述:
For each test case, output the sorting result in N lines.  That is, if C = 1 then the records must be sorted in increasing order according to ID's; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades.  If there are several students who have the same name or grade, they must be sorted according to their ID's in increasing order.
示例1

输入

3 1<br/>000007 James 85<br/>000010 Amy 90<br/>000001 Zoe 60

输出

000001 Zoe 60<br/>000007 James 85<br/>000010 Amy 90

//个人认为用java的话这道题用面向对象的思想思考更加直白,而不是单纯思考用什么样的数据结构

import java.util.*;

public class newSort {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String[] num=input.nextLine().split(" ");
int loopnum=Integer.parseInt(num[0]);
String colNum=num[1];
ArrayList<Student> data=new ArrayList<Student>();
for(int i=0;i<loopnum;i++){
String[] line=input.nextLine().split(" ");
Student student=new
Student(Integer.parseInt(line[2]),line[1],line[0]);
data.add(student);
}
if(colNum.equals("1")){
Collections.sort(data,new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
return o1.id.compareTo(o2.id);
}
});

    }else if(colNum.equals("2")){
        Collections.sort(data,new Comparator<Student>(){
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.name.equals(o2.name)){
                    return o1.id.compareTo(o2.id);
                }else{
                return o1.name.compareTo(o2.name);}
            }
        });

    }else if(colNum.equals("3")){
        Collections.sort(data,new Comparator<Student>(){
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.grade>o2.grade){
                    return 1;
                }else if(o1.grade<o2.grade){
                    return -1;
                }else{
                    return o1.id.compareTo(o2.id);
                }
             //   return o1.grade.compareTo(o2.grade);
            }
        });
    }

for(Student student:data){
System.out.println(student.id+" "+student.name+" "+student.grade);
}

}

private static boolean isRepeatInCollection(Collection<?> datas) {
    if (datas == null) {// 为空则认为不含重复元素
        return false;
    }
    if (datas instanceof Set) {//如果是set则不含有重复元素
        return false;
    }
    Set<?> noRepeatSet = new HashSet<>(datas);
    return !(datas.size() == noRepeatSet.size());
}

}

class Student{
public int grade;
public String name;
public String id;
public Student(int grade, String name, String id){
this.grade=grade;
this.name=name;
this.id=id;
}
}

发表于 2019-03-21 19:12:06 回复(0)

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
ArrayList<student> list = new ArrayList<>();
int sortNum = sc.nextInt();
for (int i = 0;i<n;i++){ list.add(new student(sc.next(),sc.next(),sc.nextInt())); } Collections.sort(list, new Comparator<student>() {
@Override
public int compare(student o1, student o2) {
if (sortNum == 1){
return o1.id.compareTo(o2.id);
}else if (sortNum == 2){
if (o1.name.equals(o2.name)){
return o1.id.compareTo(o2.id);
}else {
return o1.name.compareTo(o2.name);
}
}else {
if (o1.score == o2.score){
return o1.id.compareTo(o2.id);
}else {
return o1.score - o2.score;
}
}
}
});
for (student s:list){
System.out.println(s.id+" "+s.name+" "+s.score);
}
}
}
}
class student{
String id;
String name;
int score;
public student(String id,String name,int score){
this.id = id;
this.name = name;
this.score = score;
}
}

发表于 2018-12-03 19:51:35 回复(0)