- 分享
2023年舟山市初中生信息科技编程赛
- @ 2023-12-23 11:49:07
在此处发表你的看法以及问题以供讨论
3 条评论
-
LaoShui LV 2 SU @ 2023-12-23 18:04:29已修改
普陀二中邬疆泽思路 特产组合方案 (group.py):
构建一个二维数组,其中
dp[i][j]表示使用前i个特产是否可以凑成总价为j的组合from itertools import combinations def find_combinations(total_price, prices): n = len(prices) # 初始化动态规划数组,默认值为False dp = [[False] * (total_price + 1) for _ in range(n + 1)] # 任意i个特产的总价为0时,都可以凑成,所以将dp[i][0]设为True for i in range(n + 1): dp[i][0] = True # 填充动态规划数组 for i in range(1, n + 1): for j in range(1, total_price + 1): # 如果当前特产价格小于等于当前总价,可以选择使用或者不使用该特产 if prices[i - 1] <= j: dp[i][j] = dp[i - 1][j] or dp[i - 1][j - prices[i - 1]] else: # 如果当前特产价格大于当前总价,则只能选择不使用该特产 dp[i][j] = dp[i - 1][j] # 使用动态规划的结果来生成组合方案 result = [] for i in range(1, n + 1): for combo in combinations(prices, i): if sum(combo) == total_price: result.append(tuple(sorted(combo))) # 将组合排序后加入结果,确保输出按照价格升序排列 return result total_price = int(input()) prices = list(map(int, input().split())) result = find_combinations(total_price, prices) # 按照组合的价格升序排列输出结果 result.sort() for combination in result: print(f"{total_price}={list(combination)}") -
@ 2023-12-23 15:20:31海鲜库存计算(stock.py): yyb的一点智商,ymx的一点思路🤡
z = input().split() l_str = [] l_int = [] for i in z: if ord(i[0]) > 97: l_str.append(i) else: l_int.append(int(i)) s = [] for i in range(len(l_str)): if l_str[i] == 'in': s.append(l_int[i]) elif l_str[i] == 'out': if s[-1] > l_int[i]: s[-1] -= l_int[i] elif s[-1] == l_int[i]: s.pop(-1) else: a = l_int[i] for i in range(1,len(s)): a -= s[-1] s.pop(-1) if a <= s[-1]: break s[-1] -= a print(len(s) if s[-1] != 0 else 0) print(s[-1]) -
@ 2023-12-23 11:51:31
个人考试作答:
旅游景点正式启动日(day.py):
import sys sys.stdin = open("day.in", "r") sys.stdout = open("day.out", "w") a = int(input()) if a % 7 == 0: print("6") elif a % 7 == 1: print("sunday") elif a % 7 == 2: print("1") elif a % 7 == 3: print("2") elif a % 7 == 4: print("3") elif a % 7 == 5: print("4") elif a % 7 == 6: print("5") sys.stdin.close() sys.stdout.close()欢迎词(welcome.py):
import sys sys.stdin = open("welcome.in", "r") sys.stdout = open("welcome.out", "w") n = int(input()) for i in range(n): print("welcome") sys.stdin.close() sys.stdout.close()青少年游客平均售票价格(price.py):
import sys sys.stdin = open("price.in", "r") sys.stdout = open("price.out", "w") lst = list(map(float, input().split())) s = 0 for i in range(len(lst)): t = round(lst[i]) if t < 120: s += 0 elif t > 120 and t <= 150: s += 30 else: s += 60 # print(round(lst[i])) print(round(s / len(lst))) # print(lst) sys.stdin.close() sys.stdout.close()最多游客数量的年龄(count.py):
- 本解答有错 相同数量的,优先输出年龄大的
import sys from collections import Counter sys.stdin = open("count.in", "r") sys.stdout = open("count.out", "w") lst = list(map(int, input().split())) c = Counter(lst) a = c.most_common(1)[0][0] b = c.most_common(1)[0][1] print(a) print(b) sys.stdin.close() sys.stdout.close()
优化后:
旅游景点正式启动日(day.py):
n = int(input()) launch_day = (6 + n) % 7 if launch_day == 0: print("sunday") else: print(launch_day)青少年游客平均售票价格(price.py):
heights = list(map(int, input().split())) total_price = 0 count = 0 for height in heights: if height < 120: price = 0 elif 120 <= height <= 150: price = 30 else: price = 60 total_price += price count += 1 average_price = round(total_price / count) print(average_price)最多游客数量的年龄(count.py):
from collections import Counter lst = list(map(int, input().split())) c = Counter(lst) max_count = max(c.values()) max_ages = [age for age, count in c.items() if count == max_count] max_ages.sort(reverse=True) a = max_ages[0] b = max_count print(a) print(b)海鲜库存计算(stock.py):
lst = list(input().split(" ")) stack = [] for i in range(len(lst)): t = lst[i] if t == "in": stack.append(int(lst[i + 1])) elif t == "out": if stack: weight = int(lst[i + 1]) while weight > 0 and stack: top_weight = stack.pop() if top_weight <= weight: weight -= top_weight else: stack.append(top_weight - weight) weight = 0 remaining_boxes = len(stack) top_box_weight = stack[-1] if stack else 0 print(remaining_boxes) print(top_box_weight)特产组合方案 (group.py):
- 递归或动规做
def find_combinations(total_price, prices, current_combination, result): # 如果总价为零,说明找到了一种组合方案,将其添加到结果中 if total_price == 0: result.append(list(current_combination)) return # 如果总价为负或者特产价格列表为空,说明当前组合不符合条件,直接返回 if total_price < 0 or not prices: return # 将当前特产价格加入组合方案中 current_combination.append(prices[0]) # 递归调用,继续寻找符合条件的组合方案 find_combinations(total_price - prices[0], prices[1:], current_combination, result) # 回溯,将最后添加的特产价格从组合中移除,尝试其他可能性 current_combination.pop() # 不包含当前特产价格的情况,继续寻找其他组合方案 find_combinations(total_price, prices[1:], current_combination, result) # 输入总价和特产价格列表 total_price = int(input()) prices = list(map(int, input().split())) result = [] # 调用递归函数寻找组合方案 find_combinations(total_price, prices, [], result) for combination in result: print(f"{total_price}={combination}")
- 1