写了一天半,终于AC了……
//菜鸡养成计划
附上代码:
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node{
string card;
int score;
};
struct node2{
int id = 0;
int val = 0;
};
bool cmp1(node &p, node &q){
if(p.score != q.score) return p.score > q.score;
else return p.card < q.card;
}
bool cmp2(node2 &p, node2 &q){
if(p.val != q.val) return p.val > q.val;
else return p.id < q.id;
}
node Node[10010];
int main(){
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i ++){
cin >> Node[i].card >> Node[i].score;
}
sort(Node, Node+n, cmp1);
for(int i = 0; i < m; i ++){
int num;
string s;
cin >> num >> s;
printf("Case %d: %d %s\n", i+1, num, s.c_str());
if(num == 1){
int flag = 0;
for(int j = 0; j < n; j ++){
if(Node[j].card[0] == s[0]){
printf("%s %d\n", Node[j].card.c_str(), Node[j].score);
flag = 1;
}
}
if(flag == 0) printf("NA\n");
}
else if(num == 2){
int cnt = 0, sum = 0;
for(int j = 0; j < n; j ++){
if(Node[j].card.substr(1,3) == s){
cnt ++;
sum += Node[j].score;
}
}
if(cnt == 0) printf("NA\n");
else printf("%d %d\n", cnt, sum);
}
else if(num == 3){
node2 Node2[1010];
for(int j = 0; j < n; j ++){
if(Node[j].card.substr(4,6) == s){
int site;
sscanf(Node[j].card.substr(1,3).c_str(), "%d", &site);
Node2[site].id = site;
Node2[site].val ++;
}
}
sort(Node2, Node2+1010, cmp2);
if(Node2[0].val == 0) printf("NA\n");
else{
for(int j = 0; j < 1010; j ++){
if(Node2[j].val != 0) printf("%d %d\n", Node2[j].id, Node2[j].val);
else break;
}
}
}
}
return 0;
}
写的时候Node2忘记每次使用的时候初始化了,导致第一次交答案错了。
字符串处理的时候不需要分段存,每次用的时候substr就可以了。
第一次写的时候用的char[],导致操作不是很方便,这么一看c++真香。
终于从0学会结构体排序了。
滚去学map……
不过有没有遇到过段错误又解决了的好心人告诉我是啥问题QAQ