目錄
1,題目描述
題目大意
2,思路
注意
3,代碼
1,題目描述
Sample Input 1:
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
Sample Output 1:
000001 Zoe 60
000007 James 85
000010 Amy 90
Sample Input 2:
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
Sample Output 2:
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Sample Input 3:
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
Sample Output 3:
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90
題目大意
按照某一列(ID,名字,成績)将學生進行排序。若名字/分數相同按照ID的遞增順序排列;
2,思路
設計結構體node,存放string ID,name;int score;
将排序參考的列設定為flag;
設計排序函數cmp1,根據flag進行排序;
注意
- 逾時問題:若使用cin/cout,最後一個測試點會逾時,需用scanf/printf代替,具體如下:
id.resize(6);name.resize(8);
scanf("%s%s%d", &id[0], &name[0], &score);
3,代碼
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
int flag; //排序的标準
struct node{
string ID, name;
int score;
};
bool cmp1(node a, node b){
if(flag == 1){
return a.ID < b.ID;
}else if(flag == 2){
if(a.name < b.name) return true;
else if(a.name == b.name) return a.ID < b.ID;
}else if(flag == 3){
if(a.score < b.score) return true;
else if(a.score == b.score) return a.ID < b.ID;
}
return false;
}
int main(){
//#ifdef ONLINE_JUDGE
//#else
// freopen("1.txt", "r", stdin);
//#endif
int n, score;
node temp;
string id, name;
id.resize(6);name.resize(8);
vector<node> ans;
cin>>n>>flag;
for(int i = 0; i < n; i++){
scanf("%s%s%d", &id[0], &name[0], &score); //scanf更快 注意輸入格式
ans.push_back({id, name, score});
}
sort(ans.begin(), ans.end(), cmp1);
for(int i = 0; i < ans.size(); i++){
printf("%s %s %d\n", ans[i].ID.c_str(), ans[i].name.c_str(), ans[i].score);//printf更快 注意輸出格式
}
return 0;
}