洛谷P1051Python写法(2种)

第一种:

info = list()
money = 0
ans = 0
total = 0
for i in range(int(input())):
    info = list(input().split(" "))
    if int(info[1])>80 and int(info[5])>=1:
        money+=8000
    if int(info[1])>85 and int(info[2])>80:
        money+=4000
    if int(info[1])>90:
        money+=2000
    if int(info[1])>85 and info[4]=='Y':
        money+=1000
    if int(info[2])>80 and info[3]=='Y':
        money+=850
    if money > ans:
        ans = money
        name = info[0]
    total+=money
    money = 0
print(name)
print(ans)
print(total)

第二种:

def optiontobool(ch, yes='Y', no='N'):
    if ch == yes:
        return True
    elif ch == no:
        return False
class Student:
    def __init__(self,
  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷P1004是洛谷网站上的一个题目,题目的链接为https://www.luogu.com.***大的总价值。具体的解题思路是使用贪心算法,每次选择最少的两堆果子进行合并,然后将合并后的果子插入到合适的位置,确保数组始终是升序排列。最后,输出合并的总价值。 对于给定的输入数据,可以使用以下步骤来解决这个问题: 1. 首先,读取输入的果子数量n和每堆果子的数量。 2. 将果子数量列表进行排序。 3. 初始化一个变量ans为0,用于记录合并的总价值。 4. 使用循环,直到果子数量列表的长度大于1: a. 选择列表中最少的两堆果子mnn和fruit进行合并。 b. 使用二分搜索找到合并后的果子应该插入的位置i。 c. 将合并后的果子的价值mnn加到ans中。 d. 更新果子数量列表,将合并的果子和剩余的果子分别放到对应的位置。 5. 输出ans,即合并的总价值。 下面是一个示例代码来解决这个问题: ```python def binary_search(arr, item): low = 0 high = len(arr) - 1 while low <= high: mid = int((low + high) / 2) guess = arr[mid] if guess < item: low = mid + 1 else: high = mid - 1 return low n = int(input()) fruit = [int(i) for i in input().split()] fruit.sort() ans = 0 while len(fruit) > 1: mnn = fruit fruit += mnn i = binary_search(fruit, mnn) ans += mnn fruit = fruit[2:i] + [mnn] + fruit[i:] print(ans) ``` 以上是一个使用贪心算法解决洛谷P1004题目的Python代码。希望能对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [xml文件批量处理python脚本](https://download.csdn.net/download/caoxinri123/88239057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [洛谷P1051Python写法(2)](https://blog.csdn.net/NOIP_YYDS/article/details/122061345)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [洛谷 P1090 python](https://blog.csdn.net/weixin_52430436/article/details/122732587)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值