POJ-1245(模拟)
Programmer, Rank Thyself
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 1242 | Accepted: 467 |
Description
Implement a ranking program similar to the one used for this programming contest.
Input
The input file contains one or more contests followed by a line containing only zero that signals the end of the file. Each contest begins with a line containing a positive integer c no greater than 20 indicating the number of teams in the contest, and is followed by c lines that contain a team name and the solution times for seven problems, separated by spaces. Team names consist of between one and ten letters. All team names within a contest are unique. Times are nonnegative integers no greater than 500.
As described in the Notes to Teams, teams are ranked first by greatest number of problems solved, then by least total time, then by least geometric mean of all nonzero times. Teams that are still tied are given the same numeric ranking and are listed in alphabetical order, using case-sensitive string comparison. The numeric ranking for a team is always one more than the number of teams ranked ahead of (not tied with) that team. For this problem all geometric means will be rounded to an integer as described below, and only the rounded value will be used when computing rankings and displaying results.
If all times are zero, then the geometric mean is also zero. Otherwise, if there are n nonzero times t1, ..., tn, then the geometric mean is defined to be
exp ((ln t1 + ln t2 + ... + ln tn) / n)
where exp x means ex and ln x means the natural logarithm of x. (There are other mathematically equivalent definitions of the geometric mean, but they may produce slightly different answers due to roundoff and/or overflow problems. Use this definition to get the same answers as the judges.) After computing the geometric mean, round it to an integer by adding 0.5 and truncating any fractional digits. (C/C++ and Java automatically truncate fractions when casting a floating-point type to an integral type. In Pascal use the trunc function.)
As described in the Notes to Teams, teams are ranked first by greatest number of problems solved, then by least total time, then by least geometric mean of all nonzero times. Teams that are still tied are given the same numeric ranking and are listed in alphabetical order, using case-sensitive string comparison. The numeric ranking for a team is always one more than the number of teams ranked ahead of (not tied with) that team. For this problem all geometric means will be rounded to an integer as described below, and only the rounded value will be used when computing rankings and displaying results.
If all times are zero, then the geometric mean is also zero. Otherwise, if there are n nonzero times t1, ..., tn, then the geometric mean is defined to be
exp ((ln t1 + ln t2 + ... + ln tn) / n)
where exp x means ex and ln x means the natural logarithm of x. (There are other mathematically equivalent definitions of the geometric mean, but they may produce slightly different answers due to roundoff and/or overflow problems. Use this definition to get the same answers as the judges.) After computing the geometric mean, round it to an integer by adding 0.5 and truncating any fractional digits. (C/C++ and Java automatically truncate fractions when casting a floating-point type to an integral type. In Pascal use the trunc function.)
Output
For each contest you must output the rankings in a table. All tables will have the same width, with the length equal to the number of teams entered in the contest. Use the exact format shown in the examples. Each contest has a numbered header followed by a table with one team entry per line. Each entry contains the ranking, team name, problems solved, total time, geometric mean, and then the individual solution times in the same order they appeared in the input. In the first example all values completely fill their fields. In general you may need to pad values with spaces (never tabs) so that they have the correct field width. The team name is left-justified, and all other fields are right-justified. The ranking always has two digits, including a leading zero if necessary. Spaces will never appear at the beginning or end of lines.
Sample Input
1 Plutonians 123 234 345 456 167 278 389 4 Xap 0 0 0 0 0 0 0 Foo 20 30 0 50 40 0 10 Bar 0 50 20 0 10 40 30 Baz 0 0 0 0 0 0 0 3 Venus 213 0 0 57 0 0 0 Neptune 0 0 0 117 153 0 0 Mars 0 150 0 0 0 0 120 0
Sample Output
CONTEST 1 01 Plutonians 7 1992 261 123 234 345 456 167 278 389 CONTEST 2 01 Bar 5 150 26 0 50 20 0 10 40 30 01 Foo 5 150 26 20 30 0 50 40 0 10 03 Baz 0 0 0 0 0 0 0 0 0 0 03 Xap 0 0 0 0 0 0 0 0 0 0 CONTEST 3 01 Venus 2 270 110 213 0 0 57 0 0 0 02 Mars 2 270 134 0 150 0 0 0 0 120 02 Neptune 2 270 134 0 0 0 117 153 0 0
题意:有c个队伍,做7道题。给出做每道题的罚时,求队伍的排名。打表。排序规则如下,总题数大的排前边;罚时少的排前边;几何值小的排前边;字典序小的排前边。
思路:弄一个队伍信息的结构体。输入对应题目的罚时后,计算各支队伍总题数,总罚时,几何值。然后根据排序规则编写一个cmp函数。用sort给队伍排序就好。注意输出格式。
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <iomanip> 5 #include <algorithm> 6 using namespace std; 7 8 int c; 9 10 //队伍结构体 11 struct rank1{ 12 string s;//队名 13 int num[7];//队伍对应题的时间 14 int total;//总题数 15 int time;//总时间 16 int jihe;//几何什么东西,暂时成为几何值 17 }ra[22]; 18 19 //计算队伍总题数,总时间,几何值 20 void suan(){ 21 for (int i = 0; i < c; i++){ 22 rank1& r = ra[i]; 23 r.time = 0; 24 r.total = 0; 25 for(int j = 0; j < 7; j++){//求时间和总题数 26 if(r.num[j] != 0){ 27 r.time += r.num[j]; 28 r.total++; 29 } 30 } 31 if(r.time == 0)//求几何数 32 r.jihe = 0; 33 else{ 34 double result = 0; 35 for(int i = 0; i < 7; i++){ 36 if(r.num[i] != 0) 37 result += log(r.num[i]); 38 } 39 result /= r.total; 40 result = exp(result); 41 r.jihe = (int)(result + 0.5); 42 } 43 } 44 } 45 46 bool cmp(const rank1& r1, const rank1& r2){//比较函数 47 if(r1.total != r2.total)//先按总题数比较,题数多的排前边 48 return r1.total > r2.total; 49 if(r1.time != r2.time)//再按时间比较,时间少的排前边 50 return r1.time < r2.time; 51 if(r1.jihe != r2.jihe)//再按几何值排,几何值小的排前边 52 return r1.jihe < r2.jihe; 53 return r1.s < r2.s;//最后按字典序排。字典序小的排前边 54 } 55 56 int main() 57 { 58 int t = 1; 59 while(scanf("%d", &c), c){ 60 for (int i = 0; i < c; i++){ 61 cin >> ra[i].s; 62 for (int j = 0; j < 7; j++){ 63 cin >> ra[i].num[j]; 64 } 65 } 66 67 suan(); 68 sort(ra, ra+c, cmp); 69 70 printf("CONTEST %d\n", t++); 71 int cont = 1;//记录队伍排名 72 int contr = 1;//记录该支队伍之前有多少队伍 73 for(int i = 0; i < c; i++){ 74 if(cont/10 == 0){ 75 printf("0%d ", cont); 76 } 77 else{ 78 printf("%d ", cont); 79 } 80 cout << std::left << setw(10) << ra[i].s << " "; 81 printf("%d %4d %3d", ra[i].total, ra[i].time, ra[i].jihe); 82 for (int j = 0; j < 7; j++){ 83 printf(" %3d", ra[i].num[j]); 84 } 85 printf("\n"); 86 87 contr++; 88 if(i != c-1 && ra[i+1].total == ra[i].total && 89 ra[i+1].time == ra[i].time && ra[i+1].jihe == ra[i].jihe); 90 else{ 91 cont = contr; 92 } 93 } 94 } 95 return 0; 96 }

浙公网安备 33010602011771号