排序 1153

  1. #include <iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int maxv = 1e4 + 5;
    int n, m;
    struct Node{
           string id;
           int score;
           string type, kcID, date,ksID;
    }stu[maxv];
    //用于 3
    struct answer{
           int kc;
           int num;
    }ans[maxv];
    
    bool cmp1 (Node a, Node b){
           if(a.type != b.type) return a.type < b.type;
           else if(a.score != b.score) return a.score> b.score;
           else return a.id < b.id;
    }
    //用于 3 的时候的排序
    bool cmp2(answer a, answer b){
           if(a.num != b.num) return a.num > b.num;//考场人数
           else if(a.kc != b.kc) return a.kc < b.kc;//考场号
    }
    //查询函数
    void requiry(int type, string requ, int x){
           if(type == 1){
                  printf("Case %d: %d %s\n", x, type,requ.c_str());
                  sort(stu, stu + n, cmp1);
                  int flag = 0;
                  for(int i = 0; i < n; i++){
                         if(stu[i].type == requ){
                                flag = 1;
                                printf("%s %d\n",stu[i].id.c_str(), stu[i].score);
                         }
                  }
                  if(flag == 0) printf("NA\n");
           }else if(type == 2){
                  int flag = 0;
                  int numKC = 0, scoreKc = 0;
                  printf("Case %d: %d %s\n", x, type,requ.c_str());
                  for(int i = 0; i < n; i++){
                         if(stu[i].kcID == requ){
                                flag = 1;
                                numKC++;
                                scoreKc+=stu[i].score;
                         }
                  }
                  if(flag == 0) printf("NA\n");
                  else printf("%d %d\n",numKC,scoreKc);
    
           }else if(type == 3) {
                  int num[1000]={0};
                  int flag = 0;
                  int a;
                  printf("Case %d: %d %s\n", x, type,requ.c_str());
                  for(int i = 0; i < n; i++){
                         if(stu[i].date == requ){
                                flag = 1;
                                //字符串转化 为int
                                a = atoi(stu[i].kcID.c_str());
                                num[a]++;
                         }
                  }
                  int number = 0;
    //巧用number 作为计数 和序号的中介!!!
                  for(int k = 0; k < 1000; k++){
                         if(num[k]!=0){
                                ans[number].kc = k;
                                ans[number].num = num[k];
                                number++;
                         }
                  }
                  sort(ans,ans + number, cmp2);
                  if(flag == 0) printf("NA\n");
                  else for(int i = 0; i < number; i++){
                         printf("%d %d\n", ans[i].kc, ans[i].num);
                  }
           }
    }
    
    
    int main()
    {
           scanf("%d %d", &n, &m);
           for(int i = 0; i < n; i++){
                  cin>>stu[i].id;
                  scanf("%d", &stu[i].score);
                  stu[i].type = stu[i].id[0];
                  //截取id的子串
                  stu[i].kcID = stu[i].id.substr(1,3);
                  stu[i].date = stu[i].id.substr(4,6);
                  stu[i].ksID = stu[i].id.substr(10,3);
           }
    
    
           int typ;
           string req;
           for(int i =1; i <= m; i++){
                         //注意i 从 1开始 不是 0!
                  scanf("%d ", &typ);
                  cin>>req;
                  requiry(typ,req,i);
           }
    
        return 0;
    }
    

 

一、不会的点:

1. string 转换成 int

int a;
string b;
cin>> b;//(e.g. 输入 123)
a = atoi(b.c_str()); 
cout<<a;
//b.c_str();这个是用来将string 转换成数组用的
pringtf("%s", b.c_str());
// 输出的也是123

2.取string里的一部分存储到另外一个string里

用到substrate(a,b);

a是起点(a取 0开始到 string 长度),b是个数

 

stu[i].kcID = stu[i].id.substr(1,3);

//就是取是stu[i].id[1] 开始到 stu[i].id[3] 的子串

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值