模拟EXCEL排序(25 分)

本文介绍了一个使用C++实现的类似Excel记录排序的功能。程序能够根据用户指定的列号(如学号、姓名或成绩)对学生的记录进行排序,并考虑了相同姓名或成绩时依据学号递增排序的细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很恶心啊, 卡输入输出 ,不能用cin cout  关掉了同步也不行,估计是pta不能关掉同步

然后手动转换一下就行, 用了 const char *t = (string).c_str(); 用这个就可以很快的string 变char 不然在写排序算法的时候,判断是比较麻烦的

Excel可以对一组纪录按任意指定列排序。现请编写程序实现类似功能。

输入格式:

输入的第一行包含两个正整数N() 和C,其中N是纪录的条数,C是指定排序的列号。之后有 N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,保证没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩([0, 100]内的整数)组成,相邻属性用1个空格隔开。

输出格式:

N行中输出按要求排序后的结果,即:当C=1时,按学号递增排序;当C=2时,按姓名的非递减字典序排序;当C=3时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <math.h>
#include <stack>
#include <utility>
#include <string>
#include <sstream>
#include <cstdlib>
#include <set>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
struct Student
{
    string name;
    string id;
    int sc;
    Student(){}
    Student(string n,string ID,int num)
    {
        name = n;
        id = ID;
        sc = num;
    }
}pt[maxn];
bool cmp1(const Student a,const Student b)
{
    return a.id < b.id;
}
bool cmp2(const Student a,const Student b)
{
    if(a.name != b.name)
        return a.name < b.name;
    else
        return a.id < b.id;
}
bool cmp3(const Student a,const Student b)
{
    if(a.sc != b.sc)
        return a.sc < b.sc;
    else
        return a.id < b.id;
}
int n,c;
int main()
{
    scanf("%d %d",&n,&c);
    char Name[30];
    char ID[30];
    int num;
    for(int i = 0;i < n;i++)
    {
        scanf("%s %s %d",ID,Name,&num);
        string N = Name;
        string I = ID;
        pt[i].id = I;
        pt[i].name = N;
        pt[i].sc = num;
    }
    if(c == 1)
        sort(pt,pt+n,cmp1);
    else if(c == 2)
        sort(pt,pt+n,cmp2);
    else
        sort(pt,pt+n,cmp3);
    for(int i = 0;i < n;i++)
    {
        const char *I = pt[i].id.c_str();
        const char *N = pt[i].name.c_str();
        printf("%s %s %d\n",I,N,pt[i].sc);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值