15.05.2013 Views

Ejercicios resueltos

Ejercicios resueltos

Ejercicios resueltos

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Solución ejercicio 3. Programación dinámica (cont.)<br />

fun menu(c[1..n],Cal,m[1..n])<br />

crear D[1..n,0..Cal]<br />

desde pl ← 1 hasta n hacer<br />

D[pl,0] ← 0<br />

desde ncal ← 1 hasta Cal hacer<br />

si pl=1 Y ncal > c[1] entonces D[pl,ncal] ← +∞<br />

si no si pl=1 entonces D[pl,ncal] ← c[1]<br />

si no si ncal < c[pl] entonces<br />

D[pl,ncal] ← mín(D[pl-1,ncal],c[pl])<br />

si no<br />

D[pl,ncal] ← mín(D[pl-1,ncal],c[pl]+D[pl-1,ncal-c[pl]])<br />

fin si<br />

fin desde<br />

fin desde<br />

si D[n,Cal] < +∞ entonces obtener menu(D,c,m)<br />

devolver D[n,Cal]<br />

fin fun<br />

Yolanda García, Jesús Correas (DSIC - UCM) 14 / 32

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!