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

这是一个C++程序,用于管理一组商品,支持添加、删除商品,并根据指定条件筛选未被删除的商品。商品按评分和类型排序,删除操作通过一个单独的数据结构记录。程序能够处理多种操作并输出符合条件的商品ID。

一、题目描述

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

二、代码实现

#include<iostream>
#include<vector>
#include<set>
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历年认证考试真题解答汇总

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值