您当前的位置: 首页 >  c++

CCF小白刷题之路---201909-4 推荐系统(C/C++ 100分)

发布时间:2021-02-17 17:45:44 ,浏览量:7

一、题目描述

在这里插入图片描述 在这里插入图片描述在这里插入图片描述

二、代码实现
#include #include #include using namespace std; //商品 struct Good{ int type; int id; int score; }; //删除的商品 struct Del_Good{ int type; int id; }; set<Good> good; set<Del_Good> del_good; //运算符重载<,便于商品排序 bool operator < (const Good &g1,const Good &g2) { if(g1.score!=g2.score) return g1.score > g2.score; else { if(g1.type==g2.type) return g1.id < g2.id; else return g1.type < g2.type; } } //运算符重载<,便于商品排序 bool operator < (const Del_Good &g1,const Del_Good &g2) { if(g1.type==g2.type) return g1.id < g2.id; else return g1.type < g2.type; } int main() { int m,n; cin>>m>>n; //初始化商品 for(int i=0;i<n;i++) { Good temp; cin>>temp.id>>temp.score; for(int j=0;j<m;j++) { temp.type = j; good.insert(temp); } } int op_num; cin>>op_num; while(op_num--) { int op; cin>>op; //操作1---添加商品 if(op==1) { Good temp; cin>>temp.type>>temp.id>>temp.score; good.insert(temp); } //操作2---删除商品,统一用结构体存储 else if(op==2) { Del_Good temp; cin>>temp.type>>temp.id; del_good.insert(temp); } else { int k,type_k[m]; cin>>k; for(int i=0;i<m;i++) { cin>>type_k[i]; } vector<int> output[m]; //判断每一个商品是否被删除 for(set<Good>::iterator it = good.begin();k>0 && it!=good.end();) { if(type_k[(*it).type] > 0) { Del_Good temp; temp.type = (*it).type; temp.id = (*it).id; //find找不到返回的是end() if(del_good.find(temp)!=del_good.end()) { //这里注意it++要写在括号里面 //erase之后it所在位置为null //写在外面的话就it++就定位不到下一个了 good.erase(it++); } else { type_k[(*it).type]--; k--; output[(*it).type].push_back((*it).id); it++; } } else it++; } for(int i=0;i<m;i++) { if(output[i].size()>0) { for(int j=0;j<output[i].size();j++) { cout<<output[i][j]<<" "; } cout<<endl; } else cout<<-1<<endl; } } } } 

更多CCFCSP认证真题详解,请点击>>CCFCSP历年认证考试真题解答汇总

关注
打赏
1688896170
查看更多评论

暂无认证

  • 7浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0500s