题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

没啥难度,就一字符串题目
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;

class student{
public:   
    string name;
    int num;
    int pi;
};
//升序
bool cmp1(student x,student y){
    if(x.num<y.num){
        return false;
    }
    else if(x.num==y.num){
        return x.pi<y.pi;
    }
    else{
        return true;
    }
}
//降序
bool cmp2(student x,student y){
    if(x.num<y.num){
        return true;
    }
    else if(x.num==y.num){
        return x.pi<y.pi;
    }
    else{
        return false;
    }
}
int main(){
    string s;
    int num,f;
    cin >> num >> f;
    getline(cin , s);
    vector<student> arr;
    int count=0;
    while(getline(cin, s)){
        student temp;
        string t;
        int flag =0;
        for(auto a: s){
            if(a==' '){
                flag=1;
            }
            else if(flag == 0)
                temp.name += a;
            else if(flag == 1)
                t+=a;
        }
        int c=t.length()-1;
        temp.num=0;
        for(auto a : t){                                   
            temp.num+= (a-'0')*pow(10,c);            
            c--;
        }
        
        temp.pi=count;
        arr.push_back(temp);
        count ++;
    }
    if(f==1)
    sort(arr.begin(),arr.end(),cmp2);
    if(f==0)
    sort(arr.begin(),arr.end(),cmp1);
    
    for(auto a: arr){
        cout << a.name << ' ' << a.num << endl;
    }
}


全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 15:07
点赞 评论 收藏
分享
喜欢核冬天的哈基米很想上市:会爆NullPointerException的
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
今天投了小鹏,收到了AI面,大概会问哪些啊?
期末一定及格:总共4个部分,心理测评、行测、然后就是问岗位、对岗位的理解、过往遇到了哪些难点怎么解决,很简单,没有什么特别专业的问题,都是一些综合素质相关的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务