python_lintcode_700Cutting a Rod_418整数转罗马数字

本文探讨了切割杆问题的解决方法,通过完全背包问题的思想来寻找最大价值的切割方案,并提供了详细的代码实现。此外,还介绍了如何将整数转换为罗马数字的方法,包括对应的代码实现。

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

700Cutting a Rod

题目

Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6)

您在真实的面试中是否遇到过这个题? Yes
样例

length   | 1   2   3   4   5   6   7   8  
--------------------------------------------
price    | 1   5   8   9  10  17  17  20
Given price = [1, 5, 8, 9, 10, 17, 17, 20], n = 8
Return 22 // by cutting in two pieces of lengths 2 and 6

length   | 1   2   3   4   5   6   7   8  
--------------------------------------------
price    | 3   5   8   9  10  17  17  20
Given price = [3, 5, 8, 9, 10, 17, 17, 20], n = 8
Return 24 // by cutting in eight pieces of length 1

思路

  • 题目:给定一个长度为n,然后将其切杆和销售,根据长度和价值表,得到最大的价值。
length   | 1   2   3   4   5   6   7   8  
--------------------------------------------
price    | 1   5   8   9  10  17  17  20
Given price = [1, 5, 8, 9, 10, 17, 17, 20], n = 8
Return 22 // by cutting in two pieces of lengths 2 and 6
例如,如果杆的长度n=8,获得的最大价值是22(通过削减两块长度2和6--》5+17=22)
length   | 1   2   3   4   5   6   7   8  
--------------------------------------------
price    | 3   5   8   9  10  17  17  20
Given price = [3, 5, 8, 9, 10, 17, 17, 20], n = 8
Return 24 // by cutting in eight pieces of length 1
如果n = 8,则通过消减成8块,长度皆为1,则价值3*8=24
  • 杆可以切为多个某长度的杆,这是完全背包问题:完全背包问题每种物品可以装入无限次,将长度看做背包的容量,长度为物品的体重,不同长度的杆的价值为物品的价值。
  • 用两个循环去找到切杆为j后的最大价值,result[j]表示已切杆长j后的价值,即result[j]=max(result[j],result[j-i]+prices[i])
  • 推荐博客:http://www.jianshu.com/p/7a4e6071bc02

代码

class Solution:
    """
    @param: : the prices
    @param: : the length of rod
    @return: the max value
    """

    def cutting(self, prices, n):
        # Write your code here
        #完全背包问题
        #存放每一长的最大价值,从1---->n,初始化为0
        result = list( 0 for x in range(n+1) )
        prices = [0] + prices
        #i为每个价值下的长度
        for i in range(1,len(prices)):
            for j in range(1,n+1):
                if i <= j:
                    result[j]= max(result[j],result[j-i]+prices[i])
        return result[-1]

418整数转罗马数字

题目

给定一个整数,将其转换成罗马数字。

返回的结果要求在1-3999的范围内。

样例
4 -> IV

12 -> XII

21 -> XXI

99 -> XCIX

思路

代码

class Solution:
    """
    @param: n: The integer
    @return: Roman representation
    """
    def intToRoman(self, n):
        # write your code here
        #个位数
        ge = [0,'I','II','III','IV','V','VI','VII','VIII','IX']
        #十位数
        shi = [0,'X','XX','XXX','XL','L','LX','LXX','LXXX','XC']
        #百位数
        bai = [0,'C','CC','CCC','CD','D','DC','DCC','DCCC','CM']
        #千位
        qian= [0,'M','MM','MMM']
        #result存结果
        result = ''
        if n >=1000:
            result = result + qian[n//1000]
            n = n % 1000
        if 100 <= n <1000:
            result = result + bai[n//100]
            n = n % 100
        if 10 <= n <100:
            result = result + shi[n//10]
            n = n % 10
        if 0 < n <10:
            result = result + ge[n]
        return result
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值