题干
c++实现
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <vector>
# include <algorithm>
using namespace std;
struct student {
char name[ 16 ] ;
int score;
int seq;
} ;
bool comparefromhightolow ( student a, student b) {
if ( a. score > b. score) {
return true ;
}
else if ( a. score == b. score && a. seq < b. seq) {
return true ;
}
else {
return false ;
}
}
bool comparefromlowtohigh ( student a, student b) {
if ( a. score < b. score) {
return true ;
}
else if ( a. score == b. score && a. seq < b. seq) {
return true ;
}
else {
return false ;
}
}
int main ( ) {
int N;
scanf ( "%d" , & N) ;
vector< student> s ( N) ;
int flag;
scanf ( "%d" , & flag) ;
for ( int i = 0 ; i < N; i++ )
{
scanf ( "%s%d" , & s[ i] . name, & s[ i] . score) ;
s[ i] . seq = i;
}
if ( flag == 0 ) {
sort ( s. begin ( ) , s. end ( ) , comparefromhightolow) ;
}
else {
sort ( s. begin ( ) , s. end ( ) , comparefromlowtohigh) ;
}
for ( int i = 0 ; i < N; i++ )
{
printf ( "%s %d\n" , s[ i] . name, s[ i] . score) ;
}
return 0 ;
}