stevenjhjh 2019-12-05 20:23 采纳率: 0%
浏览 423

CCF 推荐系统问题,不知道为什么0分.

CCF 推荐系统问题
源代码如下

from heapq import *
MAXINT = 30001
MAX = 51

def opt1_add(product, category, community, score):
    product[category].append((score, MAXINT - community))


def opt2_delete(product, category, community):
    for value in range(len(product[category])):
        if product[category][value][1] == MAXINT - community:
            product[category].pop(value)
            break


# 数据存储格式为类名+分数,分数中存储响应的序号
def opt3_select(product, opt, m):
    global MAX
    global MAXINT
    constraint = opt[2:].copy()
    num = opt[1]
    result = []
    for i in range(len(constraint)):
        temp = nlargest(constraint[i], product[i])
        for key in temp:
            result.append((MAX - i, 30001 - key[1]))
    temp = {}
    for key in nlargest(num, result):
        try:
            temp[MAX - key[0]].append(key[1])
        except KeyError:
            temp[MAX - key[0]] = []
            temp[MAX - key[0]].append(key[1])
    keys = list(product.keys())
    keys.sort()
    for key in keys:
        try:
            temp2 = temp[key].copy()
        except:
            print(-1)
            continue
        temp2.sort()
        for i in range(len(temp2) - 1):
            print(temp2[i], end=" ")
        try:
            print(temp2[len(temp2) - 1])
        except IndexError:
            pass


temp = list(input().split())
m = int(temp[0])
n = int(temp[1])
product = {}

for i in range(1, n + 1):
    temp = input().split()
    for j in range(m):
        try:
            product[j].append((int(temp[1]), MAXINT - int(temp[0])))
        except KeyError:
            product[j] = []
            product[j].append((int(temp[1]), MAXINT - int(temp[0])))

num = int(input())

for i in range(num):
    opt = list(input().split())
    for j in range(len(opt)):
        opt[j] = int(opt[j])
    if opt[0] == 1:
        opt1_add(product, opt[1], opt[2], opt[3])
    if opt[0] == 2:
        opt2_delete(product, opt[1], opt[2])
    if opt[0] == 3:
        # print(product)
        opt3_select(product, opt, m)

答案与测试用例相同但是一直报错误,0分,求问什么wen'ti

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2019-12-05 23:41
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条