笔试编程题

本文解析了两道算法挑战题目,第一题涉及最大兴趣分数的计算,通过遍历和比较找到最优解;第二题则需要利用numpy进行高效的数据操作,完成一系列数值调整任务,并输出调整步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.

# -*- coding: utf-8 -*-

__author__ = 'xl'
"""
__date__ = TIME: 2018/08/11 下午12:34
describe:
"""



from functools import reduce


"""
6 3
1 3 5 2 5 4
1 1 0 1 0 0
"""


line1 = input().strip()
line2 = input().strip()
line3 = input().strip()

class_time,wake_time = map(lambda x:int(x),line1.split(" "))
interest_score_list =list(map(lambda x:int(x),line2.split(" ")))
sleep_time_list =list(map(lambda x:int(x),line3.split(" ")))

max_score = 0
for x in range(class_time-wake_time+1):
    count = 0
    origin_conut = 0
    for i in range(wake_time):
        count += interest_score_list[x+i]
    for i in range(wake_time):
        origin_conut += interest_score_list[x+i] if sleep_time_list[x+i] == 1 else 0
    score = count-origin_conut
    if max_score < score:
        max_score =score

count = 0
for index,value in enumerate(interest_score_list):
    if sleep_time_list[index] == 1:
        count += value
print(count+max_score)

 

2.

import numpy as np

line1 = input().strip().split()
max_deal = line1[1]
high = input().strip().split()
deal = []
i_deal = 0
while i_deal < max_deal:
    if  np.max(high) != np.min(high):
        i_deals = []
        mymax = np.max(high)
        myargmax = np.argmax(high)
        i_deals.append(myargmax+1)
        mymin = np.min(high)
        myargmin = np.argmin(high)
        i_deals.append(myargmin+1)
        deal.append(i_deals)
        high[myargmax] = mymax-1
        high[myargmin] = mymin+1
    else:
        break
    i_deal += 1
print(np.max(high)-np.min(high), i_deal)
for i_deals in deal:
    print(i_deals[0], i_deals[1])





# -*- coding:utf-8 -*-
#两个素数的乘积所得的自然数为半素数,给定一个数N,求小于或等于N的半素数的数目。0<N<50000
from math import sqrt,ceil
# N = int(input())
# N = input()
N = 5
def isPrime(n):
    x = int(ceil(sqrt(n)))
    if n ==2 :
        return True
    for i in range(2,x+1):
        if (n%i==0):
            return False
    return True

# ll = [n for n in range(2,2500) if isPrime(n)==True]
ll = [x for x in range(2,N//2+1) if isPrime(x)==True]

s = {x*y for x in ll for y in ll}
ss = list(s)
ss.sort()
d ={}
for index,value in enumerate(ss):
    d.update({value:index+1})


for i in range(N,2,-1):
    # print(i)
    try:
        if i <4:
            print(0)
        else:
            print(d[i])
            break
    except KeyError as e:
        pass




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值