首页 > 试题广场 >

成绩排序

[编程题]成绩排序
  • 热度指数:45126 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
用一维数组存储学号和成绩,然后,按成绩排序输出。

输入描述:
输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。


输出描述:
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
如果学生的成绩相同,则按照学号的大小进行从小到大排序。
示例1

输入

3
1 90
2 87
3 92

输出

2 87
1 90
3 92
while True:
    try:
        n=int(input().strip())
        inp=[]
        for i in range(n):
            inp.append(list(map(int,input().strip().split(' '))))
        inp=sorted(inp,key=lambda x:(x[1],x[0]))
        for i in inp:
            print(' '.join(map(str,i)))
    except:
        break
发表于 2019-08-21 00:18:42 回复(0)
while True:
    try:
        num = int(input())
        students = []
        for i in range(num):
            students.append(list(map(int,input().split())))
        students.sort(key=lambda x:(x[1],x[0]))
        for j in students:
            print('%d %d'%(j[0],j[1]))
    except Exception:
        break
编辑于 2018-10-10 23:59:27 回复(0)
#include<vector>
#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int> v(101,101);//(学号,成绩)
    for(int i=0;i<n;i++){
        int p,q;
        cin>>p>>q;
        v[p]=q;
    }
    for(int j=1;j<101;j++){
        for(int k=0;k<v.size();k++){
            if(v[k]==j){
                cout<<k<<' '<<j<<endl;
            }
        }
    }
    return 0;
}

编辑于 2018-09-03 00:05:58 回复(2)

python solution,use a dict .

while True:
    try:
        a,d=int(input()),{}
        for i in range(a):
            x,y=map(int,input().split())
            d[x]=y
        for i in sorted(d.items(),key=lambda c:c[1]):
            print(str(i[0])+" "+str(i[1]))



    except:
        break
发表于 2017-10-04 13:53:18 回复(1)
while 1:
    try:
        n=int(input())
        s=[]
        for i in range(n):
            s.append(input().split())
        s=sorted(s,key=lambda x: int(x[0]))#注意有int(),因为s中是字符,不加会按照字符排序,二者不一样
        s=sorted(s,key=lambda x: int(x[1]))
        for i in s:
            print(' '.join(i))
    except:
        break

发表于 2017-08-29 22:34:01 回复(0)
try:
    while 1:
        a = []
        for i in xrange(input()):
			a.append(map(int, raw_input().split()))
        a.sort(key=lambda x:(x[1], x[0]))
        for j, k in a:
            print j, k
except:
    pass

发表于 2016-12-29 17:27:04 回复(0)