写在前面
- ac总结
- 问题记录
- for循环过多,3 4 5 段错误、运行时异常
- cin、cout 修改为 scanf(输入无换行符,字符串输入特殊处理)、printf(字符串输出特殊处理)
- 结构体使用(传参、排序)
- 代码框架
试题详情
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
Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA
示例代码
#include <iostream>
#include<algorithm>
#include<unordered_map>
#include<vector>
using namespace std;
struct node
{
string s;
int val;
} ;
bool cmp(node l, node r)
{
if(l.val==r.val) return l.s.compare(r.s)<=0;
else return l.val>r.val;
}
void sort_print_1(struct node *sp, string tar, int pn)
{
int tar_cnt=0;
struct node ptab[pn];
for(int i=0; i<pn; i++)
if(sp[i].s.substr(0,1) == tar.substr(0,1))
{
ptab[tar_cnt++] = sp[i];
}
sort(ptab,ptab+tar_cnt,cmp);
if(tar_cnt<=0)
{
printf("NA");
}
else
{
for(int i=0; i<tar_cnt-1 ; i++)
{
printf("%s %d\n",ptab[i].s.c_str(),ptab[i].val);
}
printf("%s %d",ptab[tar_cnt-1].s.c_str(),ptab[tar_cnt-1].val);
}
}
void get_score_2(struct node *ptab, string tars, int pcnt)
{
int num=0, tsocre=0;
for(int i=0; i<pcnt; i++)
if(ptab[i].s.substr(1,3) == tars)
{
num++;
tsocre += ptab[i].val;
}
if(num<=0) printf("NA");
else printf("%d %d",num, tsocre);
}
void get_kc_rs_3(struct node *ptab, string tars, int pcnt)
{
vector<node> ans;
unordered_map<string, int> kcbh_rs;
for(int i=0; i<pcnt; i++) if(ptab[i].s.substr(4,6) == tars) kcbh_rs[ptab[i].s.substr(1,3)]++;
for(auto itr: kcbh_rs)ans.push_back({itr.first, itr.second});
sort(ans.begin(), ans.end(),cmp);
if(ans.size()<=0)
{
printf("NA");
}
else
{
int ans_size = ans.size();
for (int j = 0; j < ans_size-1; j++) printf("%s %d\n", ans[j].s.c_str(), ans[j].val);
printf("%s %d", ans[ans_size-1].s.c_str(), ans[ans_size-1].val);
}
}
int main()
{
int n,m;
scanf("%d %d", &n,&m);
char zkzh[13];
struct node tab[n];
for(int i=0; i<n; i++)
{
scanf("%s %d",zkzh, &tab[i].val);
tab[i].s = zkzh;
}
int zs;
for(int i=0; i<m; i++)
{
scanf("%d %s",&zs, zkzh);
printf("Case %d: %d %s\n",i+1,zs,zkzh);
switch(zs)
{
case 1:
sort_print_1(tab, zkzh, n);
break;
case 2:
get_score_2(tab, zkzh, n);
break;
case 3:
{
get_kc_rs_3(tab, zkzh, n);
break;
}
}
if(i<m-1) printf("\n");
}
return 0;
}
心得体会
- 磨磨唧唧,一个多月的时间,期间很长一段时间都没有认真刷题;最近一个周才开始恢复常态,经过多次尝试,才ac掉。
- 一方面,多做笔记;一方面多思考,构建自己的解题思路。
- 前几次尝试未使用第三方库,分拆了T、A、B3个数组,循环较多导致段错误、运行时错误,读者可以注意类似问题。
- 上述题目相对基础,注重细节、代码量略大。