首页 > 试题广场 >

EXCEL排序

[编程题]EXCEL排序
  • 热度指数:8094 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。     对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

输入描述:
    测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (N<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。


输出描述:
    对每个测试用例,首先输出1行“Case:”。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 
时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。
示例1

输入

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

输出

Case:
000001 Zoe 60
000007 James 85
000010 Amy 90
//用C做的  思路比较清晰  不懂得同学可以看看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student{
    char num[10];
    char name[10];
    int score;
};
int cmp1(const void *a,const void *b){
    struct Student *m=(struct Student *)a;
    struct Student *n=(struct Student *)b;
    return strcmp(m->num, n->num);
}
int cmp2(const void *a,const void *b){
    struct Student *m=(struct Student *)a;
    struct Student *n=(struct Student *)b;
    if(m->name==n->name){
        return strcmp(m->num, n->num);
    }else{
        return strcmp(m->name,n->name);
    }
   
}
int cmp3(const void *a,const void *b){
    struct Student *m=(struct Student *)a;
    struct Student *n=(struct Student *)b;
    if(m->score==n->score){
        return strcmp(m->num,n->num);
    }else{
        return m->score-n->score;
    }
}

int main() {
    int number =0;
    int way=0;
    struct Student A[100000];
    while(scanf("%d %d",&number,&way)!=EOF){
        for(int i=0;i<number;i++){
        scanf("%s %s %d",&A[i].num,&A[i].name,&A[i].score);
    }
    if(way==1){
        qsort(A,number,sizeof(struct Student),cmp1);
    }else if(way==2){
        qsort(A,number,sizeof(struct Student),cmp2);
    }else if(way==3){
        qsort(A,number,sizeof(struct Student),cmp3);
    }
    printf("Case:\n");
    for(int i=0;i<number;i++){
        printf("%s %s %d\n",A[i].num,A[i].name,A[i].score);
    }
    }
   
}

发表于 2023-03-27 17:50:47 回复(0)