每日一道算法题 成绩排序

题目

成绩排序_牛客题霸_牛客网 (nowcoder.com)

Python

n=int(input())
flag=int(input())
ans=[]
for _ in range(n):
    name,score=input().split(' ')
    ans.append([name,int(score)])
ans.sort(key=lambda x:x[1],reverse= not flag)

for e in ans:
    print(e[0],e[1],sep=' ')

C++

#include 
#include 
#include 
#include 
using namespace std;
int main()
{
    int n,flag;
    cin>>n>>flag;
    vector> res(n);
    for(int i=0;i>res[i].first>>res[i].second;
    }
    if(flag==0)
    {
        stable_sort(res.begin(),res.end(),[](const pair &front,const pair &next){
            return front.second>next.second;
        });
    }
    else if(flag==1)
    {
        stable_sort(res.begin(),res.end(),[](const pair &front,const pair &next){
            return front.second

C语言

#include

typedef struct
{
    char name[20];
    int grade;
}stu;

int main()
{
    int n,flag;
    scanf("%d",&n);
    scanf("%d",&flag);
    
    stu st[n];
    stu temp;
    int i,j;
    for(i=0;i0;i--)
        {
            for(j=0;jst[j].grade)
                {
                    temp=st[j];
                    st[j]=st[j+1];
                    st[j+1]=temp;
                }
            }
        }
    }
    if(flag==1)
    {
         for(i=n-1;i>0;i--)
        {
            for(j=0;j

你可能感兴趣的:(算法,python,开发语言)