清华大学机试 成绩排序 Easy

https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1?tpId=40&tqId=21333&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

基本思想:

弱智题,注意同名情况需要按照输入排序,加个标志位就可以了;

 关键点:

牛课的oj还要循环测case,我佛了;

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

int n;
int m;

struct node {
	int a;
	string s;
	int time;
};

bool cmp(node a,node b) {
	if (m == 0) {
		if (a.a != b.a) {
			return a.a > b.a;
		}
		else if (a.a == b.a) {
			return a.time < b.time;
		}
	}
	else {
		if (a.a != b.a) {
			return a.a < b.a;
		}
		else if (a.a == b.a) {
			return a.time < b.time;
		}
	}
}

vector<node>vec;

int main() {
	while(cin >> n >> m){
        vec.resize(0);
        for (int i = 0; i < n; i++) {
            node no;
            cin >> no.s >> no.a;
            no.time = i;
            vec.push_back(no);
        }
        sort(vec.begin(), vec.end(), cmp);
        for (auto ele : vec) {
            cout << ele.s << " " << ele.a << endl;
	    }
    }
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12385927.html