# Q1 M = 8, 10, 10, 15
# Q2 M = 9, 10, 12, 16
# Q3
def coutRuban2Produits(a0, v0, a1, v1, L):
M = 0
for nb0 in range(L//a0 + 1):
nb1 = (L - nb0*a0) // a1 prix = nb0*v0 + nb1*v1 if prix > M:
M = prix return M
# Q4
def coutRuban3Produits(a, v, L):
M = 0
for nb0 in range(L//a[0] + 1):
for nb1 in range( (L - nb0*a[0]) // a[1] + 1):
nb2 = (L - nb0*a[0] - nb1*a[1]) //
a[2]
prix = nb0*v[0] + nb1*v[1] + nb2*v[2]
if prix > M:
M = prix return M
# Q5
def coutRuban(a, v, n, L):
M = [0]
for longueur in range(1, L+1):
m = 0
for j in range(n):
if a[j] <= longueur:
if v[j] + M[longueur - a[j]] > m:
m = v[j] + M[longueur - a[j]]
M.append(m) return M[-1]
n, a, v = 3, [5,8,12], [2,4,7]
1
# Q6
def decoupageRuban(a, v, n, L):
M, D = [0], [-1]
for longueur in range(1, L+1):
m = 0
indice = -1
for j in range(n):
if a[j] <= longueur:
if v[j] + M[longueur - a[j]] > m:
m = v[j] + M[longueur - a[j]]
indice = j M.append(m)
D.append(indice)
reste_a_decouper, decoupage = L, []
while reste_a_decouper > 0:
produit = D[reste_a_decouper]
if produit == -1:
reste_a_decouper = 0 else:
decoupage.append(produit)
reste_a_decouper -= a[produit]
return decoupage, M[-1]
for L in range(30):
print('L :', L, ', coutRuban3Produits :',
coutRuban3Produits(a, v, L), ', decoupageRuban et prixTotal :', decoupageRuban(a, v, 3, L))
print('\n')
# Q7
# si i = 0 : 0
# sinon :
# 1er cas : a(i-1) > x : M(i-1,x) # 2e cas : a(i-1) <= x :
# max {M(i-1, x), v(i-1) + M(i-1, x - a(i-1))}
# Il n'est pas utile d'initialiser le cas x = 0.
2
# Q8
def coutRubanSansRepetitions(a, v, n, L):
M = [[0 for x in range(L+1)] for i in range(n+1)]
for i in range(1, n+1):
for x in range(min(L + 1, a[i-1])):
M[i][x] = M[i-1][x]
for x in range(a[i-1], L + 1):
M[i][x] = max(M[i-1][x], v[i-1] + M[i-1][x-a[i-1]])
return M[-1][-1]
for L in range(30):
print('L :', L, ', coutRuban3Produits :', coutRuban3Produits(a, v, L), ',
coutRubanSansRepetitions,',
coutRubanSansRepetitions(a, v, 3, L)) print('\n')
# Q9
def coutRubanSansRepetitionsBis(a, v, n, L):
M = [[0 for x in range(L+1)] for i in range(2)]
for i in range(1, n+1):
for x in range(min(L + 1, a[i-1])):
M[1][x] = M[0][x]
for x in range(a[i-1], L + 1):
M[1][x] = max(M[0][x], v[i-1] + M[0]
[x-a[i-1]])
M[0] = M[1][:]
return M[-1][-1]
for L in range(30):
print(coutRubanSansRepetitions(a, v, n, L), coutRubanSansRepetitionsBis(a, v, n, L))
3