解题代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct student {
char id[ 14 ] ;
int score;
int position;
int prank;
int rank;
} s[ 30000 ] ;
typedef struct student stu;
bool cmp1 ( stu a, stu b) {
if ( a. position != b. position) return a. position < b. position;
return a. score > b. score;
}
bool cmp2 ( stu a, stu b) {
if ( a. score == b. score) return ( strcmp ( a. id, b. id) < 0 ) ;
return a. score > b. score;
}
int main ( )
{
int N, K, P = 1 , index = 0 ;
scanf ( "%d" , & N) ;
for ( int i = 0 ; i < N; i++ ) {
scanf ( "%d" , & K) ;
for ( int j = 0 ; j < K; j++ ) {
scanf ( "%s %d" , s[ index] . id, & s[ index] . score) ;
s[ index] . position = P;
index++ ;
}
P++ ;
}
sort ( s, s + index, cmp1) ;
int r = 0 ;
for ( int i = 0 ; i < index; i++ ) {
if ( i && s[ i] . position != s[ i - 1 ] . position) s[ i] . prank = ( r = 1 ) ;
else if ( i && s[ i] . score == s[ i - 1 ] . score) {
s[ i] . prank = s[ i - 1 ] . prank;
r++ ;
}
else s[ i] . prank = ( ++ r) ;
}
sort ( s, s + index, cmp2) ;
for ( int i = 0 ; i < index; i++ ) {
if ( i && s[ i] . score == s[ i - 1 ] . score) s[ i] . rank = s[ i - 1 ] . rank;
else s[ i] . rank = i + 1 ;
}
printf ( "%d" , index) ;
for ( int i = 0 ; i < index; i++ )
printf ( "\n%s %d %d %d" , s[ i] . id, s[ i] . rank, s[ i] . position, s[ i] . prank) ;
return 0 ;
}
测试结果
问题整理
1.排序算法。
2.参考中在最后的总排名输出中并未记录而是直接边处理编书处,在代码上会更精简。