1153 Decode Registration Card of PAT (25 分)
A registration card number of PAT consists of 4 parts:
- the 1st letter represents the test level, namely,
T
for the top level,A
for advance andB
for basic; - the 2nd – 4th digits are the test site number, ranged from 101 to 999;
- the 5th – 10th digits give the test date, in the form of
yymmdd
; - finally the 11th – 13th digits are the testee’s number, ranged from 000 to 999.
Now given a set of registration card numbers and the scores of the card owners, you are supposed to output the various statistics according to the given queries.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers () and (), the numbers of cards and the queries, respectively.
Then lines follow, each gives a card number and the owner’s score (integer in ), separated by a space.
After the info of testees, there are lines, each gives a query in the format Type Term
, where
Type
being 1 means to output all the testees on a given level, in non-increasing order of their scores. The correspondingTerm
will be the letter which specifies the level;Type
being 2 means to output the total number of testees together with their total scores in a given site. The correspondingTerm
will then be the site number;Type
being 3 means to output the total number of testees of every site for a given test date. The correspondingTerm
will then be the date, given in the same format as in the registration card.
Output Specification:
For each query, first print in a line Case #: input
, where #
is the index of the query case, starting from 1; and input
is a copy of the corresponding input query. Then output as requested:
- for a type 1 query, the output format is the same as in input, that is,
CardNumber Score
. If there is a tie of the scores, output in increasing alphabetical order of their card numbers (uniqueness of the card numbers is guaranteed); - for a type 2 query, output in the format
Nt Ns
whereNt
is the total number of testees andNs
is their total score; - for a type 3 query, output in the format
Site Nt
whereSite
is the site number andNt
is the total number of testees atSite
. The output must be in non-increasing order ofNt
‘s, or in increasing order of site numbers if there is a tie ofNt
.
If the result of a query is empty, simply print NA
.
Sample Input:
8 4
B123180908127 99
B102180908003 86
A112180318002 98
T107150310127 62
A107180908108 100
T123180908010 78
B112160918035 88
A107180908021 98
1 A
2 107
3 180908
2 999
Sample Output:
Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA
题目解读:(太长了我就直接扒翻译了)
每个输入文件包含一个测试用例。 对于每种情况,第一行给出两个正整数N和M分别是卡数和查询数。然后N行,每行给出一个卡号和所有者得分([0,100]中的整数),中间用空格隔开。在被测者的信息之后,有M行,每行以Type Term格式提供查询,其中Type为1表示以给定水平上的所有受测者,以其分数的从小到大的顺序输出。 相应的术语将是指定级别的字母;类型为2表示在给定站点中输出受测者的总数及其总分。 相应的条款将成为站点编号;Type为3表示输出给定测试日期每个站点的受测者总数。 相应的条款将是日期,以与注册卡中相同的格式给出。
对于类型1查询,其输出格式与输入中的相同,即CardNumber Score。 如果得分相等,则按其卡号的字母升序输出(保证卡号的唯一性);
对于类型2查询,以Nt Ns格式输出,其中Nt是被测试者的总数,Ns是他们的总分;
对于类型3查询,以Site Nt格式输出,其中Site是站点编号,Nt是该站点的受测试者总数。 输出必须以Nt的非递增顺序,或者如果存在Nt,则以站点编号的递增顺序。
如果查询结果为空,则只需打印NA。
解题思路:是乙级的最后两道题,所以基本没怎么考复杂的算法和结构,就是排序。这道题卡在输出上很久,自己做的bug的地方有这几个:
1.运行超时,修改了printf和scanf后还超时,把map换成了unordered_map就不超了(记一下英文库名)
2.答案错误,因为读入日期和考场号是int型,所以还得转成3位和6位输出,高位补0,用%0xd
。(所以我为啥不用字符串读呢??)
3.之前还有一个小bug,scanf("%c",&ch)
的时候会读ch为空格,这个时候只需要在前面再补个空格,比如scanf(" %c",&ch)
就可以了。(所以我为啥不用字符串读呢!!!)
4.对于map的排序方法——可以把map的东西扔进vector再排序……另外结构体其实不需要构造函数这么麻烦的东西,vector.push_back({string,int});
这种它会自己帮你组成这个结构体。(大 好 评)
感觉这道题有很多可以化简优化的地方,看了别人的代码也受益匪浅。总之没啥难度,但是坑不少,如果做的越复杂就越容易出错。
AC代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_map>
using namespace std;
struct node {
string name;
int score;
};
bool cmp(const node &a,const node &b) {
if (a.score == b.score)
return a.name < b.name;
return a.score > b.score;
}
int main()
{
int n, m;
int sum = 0;
int count = 0;
vector<node> stu;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
char name[100];
int sc;
scanf("%s %d", name,&sc);
stu.push_back({ name, sc });
}
sort(stu.begin(), stu.end(), cmp);
for (int time = 1; time <= m; time++) {
int type;
int flag = 0;
scanf("%d", &type);
printf("Case %d: %d ", time,type);
switch (type) {
case 1:
char ch;
scanf(" %c" ,&ch);
printf("%c\n", ch);
for (int i = 0; i < n; i++) {
if (stu[i].name[0] == ch) {
printf("%s %d\n", stu[i].name.c_str(), stu[i].score);
flag = 1;
}
}
break;
case 2:
int num;
sum = 0, count = 0;
scanf(" %d", &num);
printf("%03d\n", num);
for (int i = 0; i < n; i++) {
if (stoi(stu[i].name.substr(1, 3)) == num) {
count++;
sum += stu[i].score;
flag = 1;
}
}
if (flag)
printf("%d %d\n", count, sum);
break;
case 3:
int date;
scanf(" %d", &date);
printf("%06d\n", date);
unordered_map<string, int> temp;
for (int i = 0; i < n; i++) {
if ( stoi(stu[i].name.substr(4, 6)) == date ) {
temp[stu[i].name.substr(1,3)] ++;
flag = 1;
}
}
vector<node> tempv;
for (auto& value : temp)
tempv.push_back({ value.first, value.second });
sort(tempv.begin(), tempv.end(), cmp);
for (auto& value : tempv)
printf("%s %d\n", value.name.c_str(),value.score);
break;
}
if (!flag) {
printf("NA\n");
}
}
return 0;
}
0 条评论