PAT 1153 Decode Registration Card of PAT

写了一天半,终于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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值