题目:https://pintia.cn/problem-sets/994805342720868352/problems/1071785190929788928
这道题我很久才AC,主要是不知道原来它的输入可以乱来。我原先是单独用scanf接收flag,然后再分三种情况接收后面的东西,即flag==1,接收%c;flag==2或flag==3都是用%d接收。结果一直有两个答案是错的。思考了很久觉得逻辑没有错,就只有考虑输入的东西奇形怪状,于是直接把输入整个接收,然后分情况,最后就AC了
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 10010;
int n, m;
vector<int> A, B, T;
int nt[1010] = {0}, ns[1010] = {0};
int vis[1010];struct Node{string cardnum;int score, site;
}node[maxn];int change(string a, int Left, int Right){int num = 0;for(int i = Left; i <= Right; i++)num = num * 10 + a[i] - '0';return num;
}bool cmp1(int a, int b){if(node[a].score != node[b].score)return node[a].score > node[b].score;elsereturn node[a].cardnum < node[b].cardnum;
}bool cmp2(int a, int b){if(vis[node[a].site] != vis[node[b].site])return vis[node[a].site] > vis[node[b].site];elsereturn node[a].site < node[b].site;
}int main(){scanf("%d%d", &n, &m);getchar();for(int i = 0; i < n; i++){cin>>node[i].cardnum>>node[i].score;if(node[i].cardnum[0] == 'B')B.push_back(i);else if(node[i].cardnum[0] == 'A')A.push_back(i);elseT.push_back(i);int site = change(node[i].cardnum, 1, 3);node[i].site = site;++nt[site];ns[site] += node[i].score;}sort(A.begin(), A.end(), cmp1);sort(B.begin(), B.end(), cmp1);sort(T.begin(), T.end(), cmp1);for(int i = 1; i <= m; ++i){int flag;string str;cin>>flag>>str;cout<<"Case "<<i<<": "<<flag<<" "<<str<<endl;if(flag == 1){vector<int> ans;if(str == "B")ans = B;else if(str == "A")ans = A;else if(str == "T")ans = T;if(ans.empty()){printf("NA\n");continue;}for(int k = 0; k < ans.size(); k++)cout<<node[ans[k]].cardnum<<" "<<node[ans[k]].score<<endl;}else if(flag == 2){int site;sscanf(str.c_str(), "%d", &site);if(nt[site])printf("%d %d\n", nt[site], ns[site]);elseprintf("NA\n");}else{vector<int> ans;fill(vis, vis+1010, 0);for(int j = 0; j < n; j++){if(node[j].cardnum.substr(4, 6) == str){if(!vis[node[j].site]){ans.push_back(j);}vis[node[j].site]++;}}sort(ans.begin(), ans.end(), cmp2);if(ans.empty())printf("NA\n");for(int j = 0; j < ans.size(); j++){printf("%d %d\n", node[ans[j]].site, vis[node[ans[j]].site]);}}}return 0;
}