首页 > 试题广场 >

成绩排序

[编程题]成绩排序
  • 热度指数:208291 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

给定一些同学的信息(名字,成绩)序列,请你将他们的信息按照成绩从高到低或从低到高的排列,相同成绩

都按先录入排列在前的规则处理。

例示:
jack      70
peter     96
Tom       70
smith     67

从高到低  成绩
peter     96
jack      70
Tom       70
smith     67

从低到高

smith     67

jack      70

Tom       70
peter     96

注:0代表从高到低,1代表从低到高

数据范围:人数:
进阶:时间复杂度:,空间复杂度:

输入描述:

第一行输入要排序的人的个数n,第二行输入一个整数表示排序的方式,之后n行分别输入他们的名字和成绩,以一个空格隔开



输出描述:

按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开

示例1

输入

3
0
fang 90
yang 50
ning 70

输出

fang 90
ning 70
yang 50
示例2

输入

3
1
fang 90
yang 50
ning 70

输出

yang 50
ning 70
fang 90
//纯手撸排序,用了差不多半小时才调好
#include <stdio.h>
#include <string.h>

typedef struct {
    char name[20];
    int score;
    int sort;
} sort;

int main() {
    int i, j, mod, num, temp;
    sort score[201];
    char temp_score;
    char buf[20];
    scanf("%d", &num);
    scanf("%d", &mod);
    j=num;
    for (i = 0; i < num; i++) {
        scanf("%s", score[i].name);
        scanf("%d", &score[i].score);
        if (mod == 0) {
            score[i].sort = i;
        } else {
            score[i].sort = j;
            j--;
        }
    }

    for (i = 0; i < num - 1; i++) {
        for (j = 0; j < num - 1; j++) {
            if (score[j].score < score[j + 1].score) {
                temp = score[j].score;
                score[j].score = score[j + 1].score;
                score[j + 1].score = temp;

                temp = score[j].sort;
                score[j].sort = score[j + 1].sort;
                score[j + 1].sort = temp;
                //
                strcpy(buf, score[j].name);
                strcpy(score[j].name, score[j + 1].name);
                strcpy(score[j + 1].name, buf);
            } else {
                if (score[j].score == score[j + 1].score) {
                    if (score[j].sort > score[j + 1].sort) {
                        temp = score[j].score;
                        score[j].score = score[j + 1].score;
                        score[j + 1].score = temp;

                        temp = score[j].sort;
                        score[j].sort = score[j + 1].sort;
                        score[j + 1].sort = temp;
                        //
                        strcpy(buf, score[j].name);
                        strcpy(score[j].name, score[j + 1].name);
                        strcpy(score[j + 1].name, buf);
                    }
                }
            }
        }
    }
    switch (mod) {
        case 0:
            for (i = 0; i < num; i++) {
                printf("%s %d\n", score[i].name, score[i].score);
            }
            break;
        case 1:
            for (i = num - 1; i >= 0; i--) {
                printf("%s %d\n", score[i].name, score[i].score);
            }
            break;

    }
    return 0;
}
发表于 2023-09-14 08:45:28 回复(0)
//自己写的插入、交换、归并排序都不可以,最后借助c自带qsort可以了,麻了

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct arr {
    char a[50];
    int score;
} array;
void onput(array a[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%s %d\n", a[i].a, a[i].score);
    }
}

int compar1(const void* a, const void* b) {
    array* aa = (array*)a;
    array* bb = (array*)b;
    if (aa->score != bb->score)
        return aa->score - bb->score;
    else return 0;
}
int compar2(const void* a, const void* b) {
    array* aa = (array*)a;
    array* bb = (array*)b;
    if (aa->score != bb->score)
        return bb->score - aa->score;
    else return 0;
}
void desout(array* a, int n ) {
    for (int i = n - 1; i >= 0; i--) {
        printf("%s %d\n", a[i].a, a[i].score);
    }
}
int main() {
    int i, n, op;
    scanf("%d %d", &n, &op) ;
    array* b = (array*)malloc(n * sizeof(array));
    for (i = 0; i < n; i++) {
        scanf("%s %d", b[i].a, &b[i].score);
    }
  if (op == 1) qsort(b, n, sizeof(b[0]), compar1);
    else qsort(b, n, sizeof(b[0]), compar2);
      onput(b, n);
    return 0;
}
发表于 2023-04-10 12:56:03 回复(1)
#include <stdio.h>
#include <search.h>
typedef struct student {
    char name[10];
    int score;
} stu;
stu sc[200];
int cmp1(const void* e1, const void* e2) {
    return ((stu*)e1)->score - ((stu*)e2)->score;
}
int cmp2(const void* e1, const void* e2) {
    return ((stu*)e2)->score - ((stu*)e1)->score;
}
int main() {
    int n;
    int tag;
    scanf("%d", &n);
    scanf("%d", &tag);
    for (int i = 0; i < n; ++i) {
        scanf("%s %d", sc[i].name, &sc[i].score);
    }
    if (tag == 1) {
        qsort(sc, n, sizeof(stu), cmp1);
        for (int i = 0; i < n; ++i) {
            printf("%s %d\n", sc[i].name, sc[i].score);
        }
    } else {
        qsort(sc, n, sizeof(stu), cmp2);
        for (int i = 0; i < n; ++i) {
            printf("%s %d\n", sc[i].name, sc[i].score);
        }
    }
    return 0;
}

发表于 2023-03-22 14:52:17 回复(0)
使用c语言自带的排序函数

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char buf[1024];
    int socre;
} Students;

int littecmp(const void *a, const void *b)
{
    Students *aa = (Students *)a;
    Students *bb = (Students *)b;

    if (aa->socre != bb->socre) {
        return aa->socre - bb->socre;
    }

    return 0;
}

int bigcmp(const void *a, const void *b)
{
    Students *aa = (Students *)a;
    Students *bb = (Students *)b;

    if (aa->socre != bb->socre) {
        return bb->socre - aa->socre;
    }

    return 0;
}

int main()
{
    int num, flag;
    scanf("%d", &num);
    scanf("%d", &flag);
    Students stu[200] = {0};

    for (int i = 0; i < num; i++) {
        scanf("%s %d", stu[i].buf, &stu[i].socre);
    }

    if (flag == 0) {
        qsort(stu, num, sizeof(Students), bigcmp);
    } else {
        qsort(stu, num, sizeof(Students), littecmp);
    }

    for (int i = 0; i < num; i++) {
        printf("%s %d\n", stu[i].buf, stu[i].socre);
    }

    return 0;
}
发表于 2023-02-01 22:57:28 回复(0)
#include<stdio.h>
#include <stdlib.h>
// 结构体快速排序
struct stu{
    char a[100];
    int score;
}class[1000];
int cmp1(const void*e1,const void*e2){
	return ((struct stu*)e1)->score-((struct stu*)e2)->score;
}
int cmp2(const void*e1,const void*e2){
	return ((struct stu*)e2)->score-((struct stu*)e1)->score;
}
int main(){
    int n=0;
    int b=0;
    scanf("%d\n",&n);
    scanf("%d\n",&b);
    for(int i = 0;i<n;i++){
    	scanf("%s %d",&class[i].a,&class[i].score);
	}
	if(b==1){
		qsort(class,n,sizeof(class[0]),cmp1);
	  	for(int i =0;i<n;i++){
	  		printf("%s %d\n",class[i].a,class[i].score);	
		}
	}
	if(b==0){
		qsort(class,n,sizeof(class[0]),cmp2);
	  	for(int i =0;i<n;i++){
	  		printf("%s %d\n",class[i].a,class[i].score);	
		}
	}
    return 0;
    
}

发表于 2022-08-14 21:45:17 回复(0)

在分数不超过100的情况下用hash

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct score_info {
    char *name;
    struct score_info *next;
};

struct score_info *hash[101] = {NULL};

int main()
{
    int num, cmp_func, score, end, step;
    char str[20] = {0};
    struct score_info *tmp, *cur;

    scanf("%d", &num);
    scanf("%d", &cmp_func);

    while (scanf("%s", str) != EOF && scanf("%d", &score) != EOF) {
        tmp = (struct score_info*)malloc(sizeof(struct score_info));
        tmp->name = strdup(str);
        tmp->next = NULL;
        if (!hash[score]) {
            hash[score] = tmp;
        } else {
            for (cur = hash[score]; cur->next; cur = cur->next)
                ;
            cur->next = tmp;
        }
    }

    num = cmp_func == 0 ? 100 : 0;
    end = 100 - num;
    step = cmp_func == 0 ? -1 : 1;
    while (num != (end + step)) {
        for (cur = hash[num]; cur; cur = cur->next)
            printf("%s %d\n", cur->name, num);
        num += step;
    }

    return 0;
}
发表于 2022-06-19 21:47:12 回复(0)
#include <stdio.h>
#define    N    200
typedef struct student
{
    char name[20];
    int score;
}student;
void ascend_sort(student *p,int n)
{
    int i,j;
    student temp;
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(p[j].score>p[j+1].score)
            {
                temp=p[j];
                p[j]=p[j+1];
                p[j+1]=temp;
            }
        }
    }
}
void descend_sort(student *p,int n)
{
    int i,j;
    student temp;
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(p[j].score<p[j+1].score)
            {
                temp=p[j];
                p[j]=p[j+1];
                p[j+1]=temp;
            }
        }
    }
}
int main()
{
    student stu[N];
    int i,n,flag;
    scanf("%d%d",&n,&flag);
    for(i=0;i<n;i++)
    {
        scanf("%s%d",stu[i].name,&stu[i].score);
    }
    //bubble_sort
    if(flag)            //升序
        ascend_sort(stu,n);
    else                //降序
        descend_sort(stu,n);
    for(i=0;i<n;i++)
    {
        printf("%s %d\n",stu[i].name,stu[i].score);
    }
    return 0;
}

发表于 2022-05-08 18:51:56 回复(0)
进阶要求时间复杂度是nlogn,只有快排可以,但是快排不稳定,输出的时候还得比对之前的顺序
我尝试了下,时间都花在输出了,谁有更好的办法吗
#include"stdio.h"
#include"stdlib.h"
#include"math.h"
#include"string.h"

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

void quickSort1(student * s,int first,int last){
    int i,j,privot;
    student temp;
    i = first;
    j = last;
    privot = first;
    if(first < last){
        while(i<j){ 
            while(s[i].grade <= s[privot].grade && i < last){
                i++;
            }
            while(s[j].grade > s[privot].grade){
                j--;
            }
            if(i<j){
                temp = s[i];
                s[i] = s[j];
                s[j] = temp;
            }
        }
        temp = s[j];
        s[j] = s[privot];
        s[privot] = temp;
        quickSort1(s, first, j-1);
        quickSort1(s, j+1, last);
    }
}
void quickSort0(student * s,int first,int last){
    int i,j,privot;
    student temp;
    i = first;
    j = last;
    privot = first;
    if(first < last){
        while(i<j){ 
            while(s[i].grade >= s[privot].grade && i < last){
                i++;
            }
            while(s[j].grade < s[privot].grade){
                j--;
            }
            if(i<j){
                temp = s[i];
                s[i] = s[j];
                s[j] = temp;
            }
        }
        temp = s[j];
        s[j] = s[privot];
        s[privot] = temp;
        quickSort0(s, first, j-1);
        quickSort0(s, j+1, last);
    }
}

int main(){
    int num = 0;
    int flag = 0;
    while(scanf("%d %d\n",&num,&flag)!=EOF){
        student stu[num],stu2[num];
        for(int i=0;i<num;i++){
            scanf("%s %d",stu[i].name,&stu[i].grade);
            strcpy(stu2[i].name, stu[i].name);
            stu2[i].grade = stu[i].grade;
        }
        
        //排序
        flag == 1?quickSort1(stu,0,num-1):quickSort0(stu,0,num-1);
        //恢复相同成绩的同学排序
        for(int i=0;i<num;i++){
            for(int j=0;j<num;j++){
                if(stu2[j].grade != -1 && stu2[j].grade == stu[i].grade){
                    printf("%s %d\n",stu2[j].name,stu2[j].grade);
                    stu2[j].grade = -1;
                    break;
                }
            }
        }
    }
    
    
    return 0;
}
发表于 2022-05-02 13:53:19 回复(0)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef enum{OK=0,ERROR}Status;
typedef struct NameScore
{
    char *name;
    int score;
    struct NameScore *next;
}NameScore,*NameScoreP;
NameScoreP InitLink(int up_down)
{
    NameScoreP head=NULL;
    head = (NameScoreP)malloc(sizeof(NameScore));
    head->next=NULL;
    if(up_down==1)
    head->score=-1;
    else
        head->score=101;
    return head;
}
Status InsertLink(NameScoreP head,NameScoreP e,int up_down)
{
    NameScoreP q=NULL,p=NULL;
    NameScoreP qt=NULL;
    q=head;
    qt = (NameScoreP)malloc(sizeof(NameScore));
    memcpy(qt, e, sizeof(NameScore));
    if(up_down==1)
    {
        while(e->score >= q->score && q!=NULL)
        {
            p=q;
            q=q->next;
        }
        qt->next=q;
        p->next=qt;  
    }
    else
    {
        while(e->score <= q->score && q!=NULL)
        {
            p=q;
            q=q->next;
        }
        qt->next=q;
        p->next=qt;  
    }
    return OK;
}
int main()
{
    int i,j,k;
    char name[20];
    int stu_count,up_down,namelength;
    NameScoreP NSLink=NULL;
    NameScoreP q1=NULL,q2=NULL;
    scanf("%d",&stu_count);
    scanf("%d",&up_down);
    NSLink = InitLink(up_down);
    q1 = (NameScoreP)malloc(sizeof(NameScore));
    for(i=0;i<stu_count;i++)
    {
        
        scanf("%s",name);
        namelength = strlen(name);
        q1->name = (char *)malloc(sizeof(char)*namelength);
        memcpy(q1->name, name,namelength);
        scanf("%d",&q1->score);
        InsertLink(NSLink,q1,up_down);
    }
    free(q1);
    for(i=0;i<stu_count;i++)
    {
        q1=NSLink->next;
        printf("%s %d\n",q1->name,q1->score);
        NSLink->next=q1->next;
        free(q1->name);
        free(q1);
    }
    free(NSLink);
    return 0;
}


发表于 2022-03-31 00:05:26 回复(0)
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef struct{
    char name[20];
    int score;
}Student_Def;

typedef enum{
    decrease = 0,
    increase
}Rank_Def;

int main()
{
    int num = 0;
    Rank_Def rank;
    char temp1[20] = {0};
    int temp2 = 0;
    int j = 0;
    
    while(scanf("%d", &num) != EOF)
    {
        Student_Def student_DEC[1000] = {0};
        Student_Def student_INC[1000] = {0};
        
        /*输入*/
        scanf("%d", &rank);
        for(int i=0; i <num; i++)
        {
            scanf("%s", student_INC[i].name);
            scanf("%d", &student_INC[i].score);
            /*拷贝*/
            strcpy(student_DEC[i].name, student_INC[i].name);
            student_DEC[i].score = student_INC[i].score;
        }
        
        /*插入排序,稳定,升序*/
        if(rank == increase)
        {
            for(int i=1; i < num; i++)
            {
                temp2 = student_INC[i].score;
                strcpy(temp1, student_INC[i].name);

                for(j=i-1; j >= 0; j--)
                {
                    if(temp2 < student_INC[j].score)
                    {
                        student_INC[j+1].score = student_INC[j].score;
                        strcpy(student_INC[j+1].name, student_INC[j].name);
                    }
                    else    break;
                }
                student_INC[j+1].score = temp2;
                strcpy(student_INC[j+1].name, temp1);
            }
            /*输出*/
            for(int i=0; i < num; i++)
            {
                printf("%s ", student_INC[i].name);
                printf("%d", student_INC[i].score);
                printf("\n");
            }
        }
        else /*冒泡排序,稳定,降序*/
        {
            for(int i=1; i < num; i++)
            {
                for(j=0; j < num-i; j++)
                {
                    if(student_DEC[j].score < student_DEC[j+1].score) 
                    {
                        temp2 = student_DEC[j].score;
                        student_DEC[j].score = student_DEC[j+1].score;
                        student_DEC[j+1].score = temp2;
                        strcpy(temp1, student_DEC[j].name);
                        strcpy(student_DEC[j].name, student_DEC[j+1].name);
                        strcpy(student_DEC[j+1].name, temp1);
                    }
                }
            }
            /*输出*/
            for(int i=0; i < num; i++)
            {
                printf("%s ", student_DEC[i].name);
                printf("%d", student_DEC[i].score);
                printf("\n");
            }
        }
    }
}

发表于 2021-08-05 14:51:01 回复(0)
冒泡
#include <stdio.h>
#include <string.h>
void maopao(int *chengji,char xingming[][50],int len){
//void maopao(int *arr,char xm[][50],int len){
    int arr[len];
    for(int i=0;i<len;i++)    arr[i]=chengji[i];
    char xm[len][50];
    for(int i=0;i<len;i++)    strcpy(xm[i], xingming[i]);
    for(int i=0;i<len;i++){
        for(int j=0;j<len-i;j++){
            if(arr[j]>arr[j+1]){
                int a=0;
                a=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=a;
                char str[50]="";
                strcpy(str, xm[j]);
                strcpy(xm[j], xm[j+1]);
                strcpy(xm[j+1], str);
            }
        }
    }
    for(int i=0;i<len;i++)    strcpy(xingming[i],xm[i] );
    for(int i=0;i<len;i++)    chengji[i]=arr[i];
}
int main(){
    char flage=1;
    while(flage){
        char next=0;
        int renshu=0,shunxu=0;
        
        scanf("%d%d",&renshu,&shunxu);
        if(renshu<=0)    break;
        
        char xingming[renshu][50];
        memset(xingming, '\0', renshu);
        int chengji[renshu];
        memset(chengji, 0, renshu);
        
        for(int i=0;i<renshu;i++){
            memset(xingming[i], '\0', 50);
            scanf("%s",xingming[i]);
            scanf("%d",&chengji[i]);
        }
        
        maopao(chengji, xingming, renshu);

        if(shunxu==1){
            for(int i=0;i<renshu;i++){
                printf("%s %d\n",xingming[i],chengji[i]);
            }
        }else{
            for(int i=renshu-1;i>=0;i--){
                if(chengji[i]==chengji[i-1]){
                    int j=0;
                    j=i;
                    do{
                        j--;
                    }while(chengji[j]==chengji[j-1]);
                    int z=0;
                    z=j;
                    for(j;j<=i;j++){
                        printf("%s %d\n",xingming[j],chengji[j]);
                    }
                    i=z;
                }else    printf("%s %d\n",xingming[i],chengji[i]);
            }
        }
        scanf("%c",&next);
        if(next=='\n')    flage=1;
        else              flage=0;
    }
    return 0;
}


发表于 2021-07-31 03:26:50 回复(0)
链表:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct list{
    char name[100];
    int val;
    struct list *next;
}List;
void listsort(List *head,int num,int flag)
{
    int i,j;
    char nametemp[100];
    int valtemp;
    List *p=head;
    if(flag==1)
    {
        for(i=0;i<num-1;i++)
        {
            p=head;
            for(j=0;j<num-1-i;j++)
            {
                if(p->val>p->next->val)
                {
                    strcpy(nametemp,p->name);
                    strcpy(p->name,p->next->name);
                    strcpy(p->next->name,nametemp);
                    valtemp=p->val;
                    p->val=p->next->val;
                    p->next->val=valtemp;
                }
                p=p->next;
            }
        }
    }
    else
    {
        for(i=0;i<num-1;i++)
        {
            p=head;
            for(j=0;j<num-1-i;j++)
            {
                if(p->val<p->next->val)
                {
                    strcpy(nametemp,p->name);
                    strcpy(p->name,p->next->name);
                    strcpy(p->next->name,nametemp);
                    valtemp=p->val;
                    p->val=p->next->val;
                    p->next->val=valtemp;
                }
                p=p->next;
            }
        }
    }
}
int main ()
{
    int num,flag;
    int i,j;
    while(scanf("%d",&num)!=EOF)
    {
        scanf("%d",&flag);
        char name[100][100];
        int chengji[100];
        List *ret=NULL;
        List *last=ret;
        for(i=0;i<num;i++)
        {
            scanf("%s %d",name[i],&chengji[i]);
            List *temp=malloc(sizeof(List));
            strcpy(temp->name,name[i]);
            temp->val=chengji[i];
            temp->next=NULL;
            if(ret==NULL)
            {
                ret=temp;
                last=temp;
            }
            else
            {
                last->next=temp;
                last=temp;
            }
        }
        listsort(ret,num,flag);
        List *p=ret;
        while(p!=NULL)
        {
            printf("%s %d\n",p->name,p->val);
            p=p->next;
        }
    }
    return 0;
}


发表于 2021-07-16 15:16:23 回复(0)