1. 程式人生 > >考研真題1---成績排序

考研真題1---成績排序

題目描述
查詢和排序
題目:輸入任意(使用者,成績)序列,可以獲得成績從高到低或從低到高的排列,相同成績
都按先錄入排列在前的規則處理。
例示:
jack 70
peter 96
Tom 70
smith 67
從高到低 成績
peter 96
jack 70
Tom 70
smith 67
從低到高
smith 67
Tom 70
jack 70
peter 96
輸入描述:
輸入多行,先輸入要排序的人的個數,然後輸入排序方法0(降序)或者1(升序)再分別輸入他們的名字和成績,以一個空格隔開
輸出描述:
按照指定方式輸出名字和成績,名字和成績之間以一個空格隔開
示例1
輸入

3
0
fang 90
yang 50
ning 70
輸出

fang 90
ning 70
yang 50

**這是一道考研題,使用傳統的字串形式,根據分數高低進行排序,可以實現。也可以根據C++裡面的map列表進行排序,還可以根據java裡面的集合中的list和map來做。
試著用了一下,java裡面的list列表和map做了一下,發現用list比用map簡單一些。
list版**

package com.link;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
class student
{
public String name;
public int score;
public student(String name,int score)
{
this.name=name;
this.score=score;
}
}
public class Test
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);

    while(sc.hasNext())
    {
        int num=sc.nextInt();
        int option=sc.nextInt();
        List<student> list=new ArrayList<student>();
        for(int i=0;i<num;i++)
        {
            list.add(new student(sc.next(),sc.nextInt()));
        }
        //Collections集合排序
        if(option==1)
        {
            Collections.sort(list,new Comparator<student>()
            {       
                public int compare(student o1,student o2)
                {
                    //升序
                    return o1.score-o2.score;

                }
                    });
        }
        else if(option==0)
        {
            Collections.sort(list,new Comparator<student>()
                    {       
                        public int compare(student o1,student o2)
                        {
                            //降序
                            return o2.score-o1.score;

                        }
                    });     

        }
        for(int i=0;i<list.size();i++)
        {
            System.out.println(list.get(i).name+" "+list.get(i).score);
        }
    }

}

}

map方法:
package com.map;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {

public static void main(String[] args) {
    Map<String,Integer> ms=new TreeMap<String,Integer>();
    Scanner s=new Scanner(System.in);
    int m=s.nextInt();

    int sel=s.nextInt();
    int i=1;
    while(i<=m)
    {
        String s1=s.next();
        int s2=s.nextInt();
        ms.put(s1, s2);
        i++;

    }
    List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(ms.entrySet());
    if(sel==1)
    {

        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
        @Override
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            //升序
            return o1.getValue()-o2.getValue();
        }
    });
    }
    else if(sel==0)
    {
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                //降序
                return o2.getValue()-o1.getValue();
            }
        });
    }
    for (int j=0;j< list.size(); j++) {
        Map.Entry<String, Integer> map = list.get(j);
        System.out.println(map.getKey()+" "+map.getValue());
    }
}

}