高级算法设计Project

Project 1:
1.Minimum parcel assignment
1.1 Problem describe
A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6*6. Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.
Input: The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size 1*1 to the biggest size 6*6. The end of the input file is indicated by the line containing six zeros.
Output: The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last “null” line of the input file.

中文简述:
在6*6的大箱子里面放小箱子,怎样放置才能使得所需要的大箱子数目为最小?

其求解思路为贪心算法的实现:
There are six kind of packets, we should put all of them in the parcels which is 6*6 and need as few as possible. We can use the main idea of Greedy algorithm: put the biggest products in the parcels and fulfill the rest of space which as big products as possible.
First, when we put 4*4, 5*5 and 6*6 products in the parcels, then one parcel must be occupied. In this case, we should fulfill the parcels with 2*2 and 1*1. As for the 3*3 products, every four of them can fulfill a parcel. When the number of 3*3 is different, the number of 2*2 products which concerned is also different. Last, when there is still remain 2*2 and 1*1 products, we can assign them according to the space-distribution. Counting all the parcels we need, then we can have the solution .
Here is the fulfill strategy:
6*6 no need to fulfill
5*5 fulfill with 1*1
4*4 fulfill with 2*2, then with 1*1
3*3 fulfill with 2*2, then with 1*1
2*2 fulfill with 1*1
1*1 no need to fulfill

代码实现如下:

# -- coding: utf-8 --
#贪心算法

def Assignment(s):
    num_3 = [0,5,3,1] # 3的四种情况,方便讨论
    count = (s[5]+s[4]+s[3]+(s[2]+3)/4) #6,5,4,3所需要的箱子数,其中3最多可以放四个
    remain_2 = s[3]*5+num_3[s[2]%4] #剩余的2的放置情况
    if s[1]>remain_2:
        count += (s[1]-remain_2+8)/9
    remain_1 = count*36 - s[5]*36 - s[4]*25 - s[3]*16 - s[2]*9 - s[1]*4 #剩余的1的放置情况
    if s[0]>remain_1:
        count += (s[0]-remain_1+35)/36
    return count

if __name__=="__main__":
    s =[]
    i=0
    s.append(input())
    while (sum(s[i])!= 0):
        s.append(input())
        i += 1
    for i in range(len(s) - 1):
        print Assignment(s[i])


Project 2:

2.Dynamic programming
2.1 Problem describe
A string “s” is consisted of number characters. If the substring value of one or two adjacent characters in “s” is between 1 and 26, the substring can be converted to an alphabet. The rule is set as follows: “1” is converted to “A”, “2” to “B”, “3” to “C” … and ”26” to “Z”. Please find out how many different results can string “s” be converted to.
(there is no corresponding letter to “0”)
Input: s=”123”
The results can be: ”ABC”, “LC”, “AW” (“23” to “W”, “12” to “L”)
Output: 3

这里用到的思想为动态规划的理念。问题为“爬楼梯”问题的变形,即爬楼梯的过程中,这个楼梯能不能爬需要另外做判断。

Suppose that cp[i] be the number of existing alphabets. Because the number of alphabet k: (), and alphabet can be only expressed by 1 or 2 numbers, therefore :
cp[i] = cp[i-1]* + cp[i-2]*

代码如下:

#coding:utf-8
#运用爬楼梯中斐波那契数列的含义,对数字和字母问题进行动态规划方式的解决

def check(num):#判断函数
    if int(num)>26 or num.startswith('0'):
        return 0
    else:
        return 1


def word_count(s):
    cp = [0]
    if s.startswith('0') or not s.isdigit():        #条件判断:输入数字开头为0和带有字母的输入皆为非法输入
        return -1
    if len(s)==1:
        cp.append(check(s[0]))
    else:
        cp.append(check(s[0]))
        cp.append(check(s[0])*check(s[1])+check(s[:2]))     #检测前两个数字的情况
        for i in range(3,len(s)+1):
            t1 = check(s[i-1])
            t2 = check(s[i-2:i])
            cp.append(t1*cp[i-1]+t2*cp[i-2])
    return cp[len(s)]



if __name__=="__main__":
    ss = str(raw_input("Please input a number:"))
    print word_count(ss)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值