HDU2093--考試排名

考試排名

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2444 Accepted Submission(s): 864
 
Problem Description
C++編程考試使用的實時提交系統,具有即時獲得成績排名的特點。它的功能是怎麼實現的呢?
我們做好了題目的解答,提交之後,要麼“AC”,要麼錯誤,不管怎樣錯法,總是給你記上一筆,表明你曾經有過一次錯誤提交,因而當你一旦提交該題“AC”後,就要與你算一算帳了,總共該題錯誤提交了幾回。雖然你在題數上,大步地躍上了一個臺階,但是在耗時上要攤上你共花去的時間。特別是,曾經有過的錯誤提交,每次都要攤上一定的單位時間分。這樣一來,你在做出的題數上,可能領先別人很多,但是,在做出同樣題數的人羣中,你可能會在耗時上處於排名的劣勢。
例如:某次考試一共8題(A,B,C,D,E,F,G,H),每個人做的題都在對應的題號下有個數量標記,負數表示該學生在該題上有過的錯誤提交次數,但到現在還沒有AC,正數表示AC所耗的時間,如果正數a跟上一對括號,裏面有個整數b,那就表示該學生提交該題AC了,耗去了時間a,同時,曾經錯誤提交了b次,因此對於下述輸入數據:



若每次錯誤提交的罰分爲20分,則其排名從高到低應該是這樣的:
Josephus 5 376
John 4 284
Alice 4 352
Smith 3 167
Bob 2 325
Bush 0 0
 
Input
輸入數據的第一行是考試題數n(1≤n≤12)以及單位罰分數m(10≤m≤20),每行數據描述一個學生的用戶名(不多於10個字符的字串)以及對所有n道題的答題現狀,其描述採用問題描述中的數量標記的格式,見上面的表格,提交次數總是小於100,AC所耗時間總是小於1000。

 
Output
將這些學生的考試現狀,輸出一個實時排名。實時排名顯然先按AC題數的多少排,多的在前,再按時間分的多少排,少的在前,如果湊巧前兩者都相等,則按名字的字典序排,小的在前。每個學生佔一行,輸出名字(10個字符寬),做出的題數(2個字符寬,右對齊)和時間分(4個字符寬,右對齊)。名字、題數和時間分相互之間有一個空格。
 
Sample Input
8 20
Smith	  -1	-16	8	0	0	120	39	0
John	  116	-2	11	0	0	82	55(1)	0
Josephus  72(3)	126	10	-3	0	47	21(2)	-2
Bush	  0	-1	-8	0	0	0	0	0
Alice	  -2	67(2)	13	-1	0	133	79(1)	-1
Bob	  0	0	57(5)	0	0	168	-7	0
 
Sample Output
Josephus    5  376
John        4  284
Alice       4  352
Smith       3  167
Bob         2  325
Bush        0    0
 

解析:這道題在杭電上屬於簡單題,但是做起來好麻煩,最噁心的就是格式問題了,不過涉及的東西還比較多,我覺得不失爲經典題哈!
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using std::endl;
using std::cin;
using std::cout;
using std::string;
using std::sort;
using std::setw;
using std::ios;
const int MAXN = 1000;
struct student{
	string name;
	int numSolved;
	int time;
}stu[MAXN];
//處理字符串的函數
int strtoint(string s , int penaltyTime)
{
	int value=0 , sum=0;
	for(int i=0; i<s.length(); ++i)
	{
		if(s[i]=='(')
		{
			sum+=value;
			value = 0;
		}else if(s[i]==')')
		{
			sum+=(value*penaltyTime);
		}else{
			value=value*10+(s[i]-'0');
		}	
	}
	return sum > value ? sum:value;
}
//排序函數
bool cmp(student a , student b)
{
	if(a.numSolved > b.numSolved)
		return true;
	if(a.numSolved == b.numSolved)
		return a.time < b.time;
	if(a.numSolved==b.numSolved && a.time == b.time)
		return a.name < b.name;
	return false;
}
int main()
{
#ifdef LOCAL
	freopen("input.txt" , "r" , stdin);
#endif
	int n , penaltyTime;
	cin >> n >> penaltyTime;
	string name , str;
	int m = 0;
	//輸入
	while(cin >> name)
	{
		stu[m].name = name;
		for(int i=0; i<n; ++i)
		{
			cin >> str;
			if(str[0] >'0')
			{
				stu[m].numSolved++;
				stu[m].time += strtoint(str , penaltyTime);
			}
		}
		m++;
	}
	//排序
	sort(stu , stu+m , cmp);
	//輸出
	for(int i=0; i<m; ++i)
	{
		//控制左對齊
		cout.flags(ios::left);
		//控制佔用字符
		cout << setw(10) << stu[i].name << " ";
		//控制右對齊
		cout.flags(ios::right);
		//控制字符和輸出格式
		cout << setw(2) << stu[i].numSolved << " ";
		cout << setw(4) << stu[i].time;
		cout << endl;
	}
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章