PAT 1153
C++
版【存在问题】
1.题意
给出一串参加pat考试的人的名单。接着给出一系列查询的名单,让你输出其查询结果。
2.分析
题目比较繁琐。我的主要思想如下:
- 针对不同的输入type,进行不同的测试程序,得到不同的执行结果。
3.代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<iostream>
using namespace std;
struct student{
char regId[15];
int score;
};
struct result3{
int site;
int num;
};
student stu[10005];
student res[10005];
result3 res3[10005];
int cmp(student s1,student s2){
if(s1.score == s2.score) return strcmp(s1.regId,s2.regId) < 0;
return s1.score > s2.score;
}
int cmp2(result3 r1,result3 r2){
if(r1.num == r2.num) return r1.site< r2.site;
return r1.num > r2.num;
}
int main(){
int N,M;
cin >> N>> M;
int i ,j;
for(i = 0;i< N;i++){
cin >> stu[i].regId >> stu[i].score;
}
int type;
char level;//如果type是1的时候,则输入level
int term;//如果tupe是2/3 ,则输入term
int flag ;
for(i = 0;i< M;i++){
flag = 0;
cin >> type;
if(type == 1){
cin >> level;//开始处理
int index = 0;
cout << "Case "<< i+1 <<": "<<type << " " << level<< "\n";
for(j = 0;j< N;j++){
if(stu[j].regId[0] == level){
flag = 1;//说明有
strcpy(res[index].regId,stu[j].regId);
res[index].score = stu[j].score;
index ++;
}
}
if(flag == 0){
cout << "NA" << "\n";
}else{
sort(res,res+index,cmp);
for(int k = 0;k< index;k++){
cout << res[k].regId << " "<< res[k].score<<"\n";
}
}
}
else if(type == 2){
cin >> term;
int testees = 0;
int sum = 0;
int site = 0;
for(j = 1;j < N;j++){
site = (stu[j].regId[1]-'0') * 100
+ (stu[j].regId[2] -'0') * 10
+ (stu[j].regId[3] -'0');
if(site == term){
flag = 1;
testees ++;
sum += stu[j].score;
}
}
cout << "Case "<< i+1 <<": "<<type << " "<<term<<"\n";
if(flag == 1) cout << testees << " "<< sum << "\n";
else cout << "NA" << "\n";
}
else if(type == 3){
cin >> term;
int array[1000];
memset(array,0,sizeof(array));
int date = 0;
int site= 0;
int index = 0;
for(j = 0;j< N;j++){
date = (stu[j].regId[4]-'0') * 100000
+ (stu[j].regId[5] -'0') * 10000
+ (stu[j].regId[6] -'0') * 1000
+ (stu[j].regId[7] -'0') * 100
+ (stu[j].regId[8] -'0') * 10
+ (stu[j].regId[9] -'0');
if(date == term){
flag = 1;
site = (stu[j].regId[1]-'0') * 100
+ (stu[j].regId[2] -'0') * 10
+ (stu[j].regId[3] -'0');
array[site]++;
}
}
cout << "Case "<< i+1 <<": "<<type << " "<< setfill('0')<<setw(6)<<term<<"\n";
if(flag == 1){
for(j = 0;j< 1000;j++){
if(array[j]!=0){
res3[index].num = array[j];
res3[index].site = j;
index++;
}
}
sort(res3,res3+index,cmp2);
for(j = 0;j < index;j++){
cout << res3[j].site <<" "<< res3[j].num << "\n";
}
}
else cout << "NA" << "\n";
}
}
}
4.测试用例
5.执行结果
- 代码未完全AC。不大清楚哪里出了问题。
- 因为嫌弃代码写的太丑,我将如下的代码
date = (stu[j].regId[4]-'0') * 100000
+ (stu[j].regId[5] -'0') * 10000
+ (stu[j].regId[6] -'0') * 1000
+ (stu[j].regId[7] -'0') * 100
+ (stu[j].regId[8] -'0') * 10
+ (stu[j].regId[9] -'0');
换成了
temp = stu[j].regId;
date = stoi(temp.substr(4,6));
代码倒是简洁了,但是却没有过一个点。【运行超时】看来还是数组的运行效率高 (╯﹏╰)b
== update on 20190506 ==
今天重新写了一下该题,真是麻烦,如果考场上遇到这样的题,还真不知道怎么迅速的写完。。。
如下的代码能够AC
#include<cstdio>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#define sm 1000 //site maxn
#define nm 10005 // n maxn
using namespace std;
struct Node {
int site;//考场
int id;//考生编号
int score;//考生分数
int date;//考生日期
string level;//考试等级
string card;//考生card
};
struct result{
int site;//考场
int sum = 0;//考场的人数
};
//<int>里面放的是日期,value是一个vector<node>
map<int,vector<Node>> dateRec;//各个日期的考生
vector<Node> TRec;//T层级的考生
vector<Node> ARec;//A层级的考生
vector<Node> BRec;//B层级的考生
int sitePeo[sm];//表示每个考场的人数
int siteSco[sm];//该考场的分数
Node node[nm];
bool cmp1(Node n1,Node n2){
if(n1.score == n2.score) return n1.card < n2.card;
return n1.score > n2.score;
}
bool cmp2(result r1, result r2){
if(r1.sum == r2.sum ) return r1.site < r2.site;
return r1.sum > r2.sum;
}
void print(string inLevel){
if(inLevel.compare("T") == 0) {
if(TRec.size()==0) cout <<"NA\n";
for(int i = 0;i < TRec.size();i++){
cout << TRec[i].card <<" "<< TRec[i].score<<"\n";
}
}
else if(inLevel.compare("A") == 0) {
if(ARec.size()==0) cout <<"NA\n";
for(int i = 0;i < ARec.size();i++){
cout << ARec[i].card <<" "<< ARec[i].score<<"\n";
}
}
else if(inLevel.compare("B") == 0){
if(BRec.size()==0) cout <<"NA\n";
for(int i = 0;i < BRec.size();i++){
cout << BRec[i].card <<" "<< BRec[i].score<<"\n";
}
}
}
//解决case 3的情况
void solve3(vector<Node> &vn){
int i;
result res[sm] ;
for(i = 0;i < vn.size();i++){
//cout << vn[i].site <<" ++ ";//输出当前的考场
res[vn[i].site].sum++;//总人数相加
res[vn[i].site].site = vn[i].site;
}
sort(res,res+sm,cmp2);
i = 0;
while(res[i].sum!=0){
cout << res[i].site <<" "<< res[i].sum<<"\n";
i++;
}
}
int main(){
int N,M;
cin >> N >> M;
int i,j;
string card;
int score;
int tempSite,tempDate,tempId;//临时考场 临时日期 临时Id
Node tempNode;
fill(sitePeo,sitePeo+sm,0);
fill(siteSco,siteSco+sm,0);
for(i = 0;i< N;i++){
cin >> card >> score;
string level;//表示考试等级
tempNode.level = card.substr(0,1);
level = card.substr(0,1);
tempNode.site = stoi(card.substr(1,3));
tempNode.date = stoi(card.substr(4,6));
tempNode.id = stoi(card.substr(10,3));
tempNode.card = card;//赋值card
tempNode.score = score;//赋值score
//搞定输入1:针对不同的级别,放入到不同的vector中
if(level.compare("T") == 0) TRec.push_back(tempNode);
else if(level.compare("A") == 0) ARec.push_back(tempNode);
else if(level.compare("B") == 0) BRec.push_back(tempNode);
//搞定输入2
sitePeo[tempNode.site]++;
siteSco[tempNode.site] += score;
//搞定输入3
dateRec[tempNode.date].push_back(tempNode);
}
//将数据排序
sort(TRec.begin(),TRec.end(),cmp1);
sort(ARec.begin(),ARec.end(),cmp1);
sort(BRec.begin(),BRec.end(),cmp1);
int count = 1;
while(count <= M) {
int tempSite,tempDate,tempId;//临时考场 临时日期 临时Id
int type;
string inLevel;
cin >> type;
switch(type){
case 1:
cin >> inLevel;
cout <<"Case "<<count<<": "<<type<<" "<<inLevel<<"\n";
print(inLevel);
break;
case 2:
cin >> tempSite;
cout <<"Case "<<count<<": "<<type<<" "<<tempSite<<"\n";
if(sitePeo[tempSite] == 0) cout <<"NA\n";
else cout << sitePeo[tempSite] <<" "<< siteSco[tempSite]<<"\n";
break;
case 3:
cin >> tempDate;
printf("Case %d: %d %06d\n",count,type,tempDate); //注意输出格式
vector<Node> vn;
if(dateRec.find(tempDate)!=dateRec.end()){
vn = dateRec[tempDate];//赋值到vn中
solve3(vn);
}
else{
cout <<"NA\n";
}
}
count++;
}
}
如下给出部分测试用例
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
8 3
B123180908127 99
B102180908003 86
A112180318002 98
A107150310127 62
A107180908108 100
A123180908010 78
B112160918035 88
A107180908021 98
1 A
1 T
3 020302