单数组多重存储学生数据(人话:就多加了个科目,其实没什么)

具体的一些细节放在注释里面了,都挺弱智的细节,但是容易忽视,下次别做弱智

#include<iostream>
#include<algorithm> //为什么用它就不用多说了吧,你都用了algorithm,不就是sort函数啊
#include<string> //注意啊,你用了string类型,要用这个啊,只要涉及到字符串,你用用准没错
using namespace std;
const int N =1000010;
struct Student
{
    Student(){}
    int chinese,math;
    string name; //在这里需要注意一些小细节,就是这个位置输入的是一串字符串,如果选择用char则必须每个单词后面跟一个‘/0’
}stu[N];
bool cnm(Student a,Student b)
{
    if(a.chinese !=b.chinese)
    {
        return a.chinese>b.chinese;
    }
    else
    {
        return a.name < b.name;
    }
}
bool cnm2(Student a,Student b)
{
     if(a.math !=b.math)
    {
        return a.math>b.math;
    }
    else
    {
        return a.name < b.name;
    }
}
int main()
{
    int number =0,choose =0;
    cin>>number>>choose;
    for(int i =0;i<number;i++)
    {
        cin>>stu[i].name>>stu[i].chinese>>stu[i].math; //仔细敲一敲,别躲敲多了点(.)
    }
    if(choose ==1)
    {
        sort(stu,stu+number,cnm);
    }
    else{
        sort(stu,stu+number,cnm2);
    }

    for(int i =0;i<number;i++)
    {
        cout<<stu[i].name<<" "<<stu[i].chinese<<" "<<stu[i].math<<endl;//注意细节,中间需要输出空格哟!
    }
    return 0;
}

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值