描述
每學期考試結束,教務處老師都要對學生成績進行一些統計。請你幫助教務處老師對學生成績從高到低進行排名。成績相同,排名序號相同,先後出現順序與原始次序相同。
輸入
第一行是一個正整數:測試用例數目,最多爲100。之後,每個測試用例包括:
l 第1行,含一個整數n:1≤n≤100,表示學生數
l n行,每一行含一個字符串(長度不超過10)、一個整數,分別表示學生的學號、成績
輸出
對於每個測試用例:
l 輸出“Case #:”,#表示序號
l 輸出n行,每一行含排名序號、學生學號、成績
注意:輸出部分的結尾要求包含一個多餘的空行。
樣例輸入
2
5
B05021924 90
B07030901 91
B07030419 90
B07021027 93
B06060606 87
1
D0408 100
樣例輸出
Case 1:
1 B07021027 93
2 B07030901 91
3 B05021924 90
3 B07030419 90
5 B06060606 87
Case 2:
1 D0408 100
一開始通過sort函數來進行排序,可是一直無法通過,鬱悶了好一陣。後來在網上找到一種解法是用key-value的數據結構做的,其實就是哈希表中的拉鍊法。
#include <iostream>
#include <string>
using namespace std;
class student //定義學生節點,C++中struct和class差別不大。
{
public:
string id;
int num; //只在主表中會用到,用來存放節點數
student *next;
};
int _tmain(int argc, _TCHAR* argv[])
{
int N,n,t_result,tmp=0;
int count;
string t_id;
student vec[101];//哈希表的主表
student *p,*stu;
cin>>N;
for(int i=1;i<N+1;++i)
{
cin>>n;
for(int x=0;x<101;++x)
{
vec[x].num=0;
vec[x].next=NULL;
}
while(n--)
{
cin>>t_id>>t_result;
stu =new student;//節點
stu->id=t_id;
stu->next=NULL;
p=&vec[t_result];
while(p->next!=NULL)
p=p->next;
p->next=stu;
vec[t_result].num++;
}
cout<<"Case "<<i<<":"<<endl;
count=1;
for(int j=100;j>=0;--j)
{
if(vec[j].next!=NULL)
{
tmp=vec[j].num;
p=vec[j].next;
while(p!=NULL)
{
cout<<count<<" "<<p->id<<" "<<j<<endl;
p=p->next;
}
}
else
continue;
count +=tmp;
}
}
return 0;
}