【一隻蒟蒻的刷題歷程】 【PAT】 A1025 PAT排名

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4


題目大意:

編程能力測試(PAT)由浙江大學計算機科學與技術學院組織。每個測試應該在多個地方同時運行,並且等級列表將在測試後立即合併。現在,您的工作就是編寫一個程序來正確合併所有排名列表並生成最終排名。
輸入規格:

每個輸入文件包含一個測試用例。對於每種情況,第一行都包含一個正數N(≤100),即測試位置的數量。然後是N個等級列表,每個等級列表都包含一個正整數K(≤300),受測試者人數的行,然後是包含註冊編號(13位數字)和每個受測試者總分的K行。一行中的所有數字都用空格分隔。
輸出規格:

對於每個測試用例,首先在一行中打印被測試者的總數。然後以以下格式打印最終排名列表:

registration_number final_rank location_number local_rank

位置的編號從1到N。輸出必須按最終等級的降序排列。具有相同分數的被測者必須具有相同的等級,並且輸出必須以其註冊號的降序排列。

樣本輸入:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

樣本輸出:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4


思路:

結構體排序
直接這樣不知道爲什麼就是錯的,非得把第一個數先賦值纔可以

for(int i=0;i<cnt;i++)
   {
   	  if(stu[i].score == stu[i-1].score && i-1>=0) stu[i].final=stu[i-1].final;
   	  else stu[i].final = i+1;
   	  //c++;
   }

代碼:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
struct node{
	string id;    //編號 
	int final;    //總排名 
	int local;    //該等級排名 
	int score;    //分數 
	int grade;    //等級 
}stu[30100]; 
bool cmp(node a,node b)   //排序方式
{
	if(a.score != b.score) return a.score > b.score;
	else  return a.id < b.id;

}
int n,k;
int main() 
{
   cin>>n;
   int cnt=0,nowcnt=0;  //總的人數,該等級開始的位置
   for(int grade=1;grade<=n;grade++) //等級
   {
   	  cin>>k;
	  for(int i=0;i<k;i++)
	  {
	  	 cin>>stu[cnt].id>>stu[cnt].score;
	  	 stu[cnt].grade = grade; 
	  	 cnt++;
	  } 
	 // int c=1;
	  sort(stu+nowcnt,stu+cnt,cmp); //得出local的排名
	  stu[nowcnt].local = 1;
	  for(int j=nowcnt+1;j<cnt;j++)
	  {
	  	if(stu[j].score == stu[j-1].score) stu[j].local=stu[j-1].local;
   	    else stu[j].local = j-nowcnt+1;
   	    //c++;
	  }
	  nowcnt = cnt;
   }
   
   sort(stu,stu+cnt,cmp);  //得出總排名
   //int c=1,last; 這個方法也不行,直接一個個賦值
   stu[0].final = 1;
   for(int i=1;i<cnt;i++)
   {
   	  if(stu[i].score == stu[i-1].score) stu[i].final=stu[i-1].final;
   	  else stu[i].final = i+1;
   	  //c++;
   }
   
   cout<<cnt<<endl;
   for(int i=0;i<cnt;i++)
    cout<<stu[i].id<<" "<<stu[i].final<<" "<<stu[i].grade<<" "<<stu[i].local<<endl;
    return 0;
}

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