算法作业3--最优合并问题

import re
from random import randint

A = []
MaxNum = 999999
MinNum = 0
#  输入堆数x回车,生成x个随机数
inputFile = open('input.txt', 'w')
inputFile.close()
inputFile = open('input.txt', 'a')
print('输入序列数')
x = input()
inputFile.write(x + '\n')
for i in range(int(x)):
    s = str(randint(1, 10)) + ' '
    inputFile.write(s)
inputFile.close()
#  读取input.txt中的数据并生成列表
inputFile = open('input.txt')
inputContent = inputFile.readlines()
print(inputContent[1])
x = inputContent[0]  # 序列个数x
stonesNumRegex = re.compile(r'\d+')
mo = stonesNumRegex.findall(inputContent[1])
A.extend(mo)
B = list(map(int, A))
n = int(x)


def s_sort(B, n):
    g1 = 0
    i = 0
    B.sort()
    while i < n - 1:
        g1 = g1 + B[i] + B[i + 1] - 1;
        B[i] = B[i] + B[i + 1];
        B[i + 1] = 0
        i += 1
        B.sort()

    print('最小得分为' + str(g1))


def b_sort(B, n):
    g2 = 0
    j = 0
    B.sort(reverse=True)
    for j in range(n - 1):
        g2 = g2 + B[0] + B[1] - 1
        B[0] = B[0] + B[1]
        B[1] = 0
        j += 1
        B.sort(reverse=True)
        
    print('最大打分为' + str(g2))


s_sort(B, n)
b_sort(B, n)

算法六组

自己的版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值