Advertisement
Guest User

DP - Sencillar - Bottom_Up

a guest
Oct 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. #define CantDen 6 //Cantidad de Denominaciones
  7.  
  8. short monedas[CantDen] = { 1,5,10,20,25,50 };
  9.  
  10. struct sencillo {
  11. short cant = 0;
  12. short moneda = 0;
  13. sencillo(short cant = SHRT_MAX, int moneda = 0) : cant(cant), moneda(moneda) {};
  14. };
  15.  
  16. sencillo valores[100];
  17.  
  18. inline void minimoSencillo(int valor) {
  19. for (short i = 0; valor >= monedas[CantDen] && i < 6; i++) {
  20. short moneda = monedas[i];
  21. short cantidad = 1 + valores[valor - moneda].cant;
  22. if (valores[valor].cant > cantidad) {
  23. valores[valor].cant = cantidad;
  24. valores[valor].moneda = moneda;
  25. }
  26. }
  27. }
  28.  
  29. int main() {
  30. valores[0].cant = 0;
  31. short valor;
  32. for (valor = 1; valor < 100; valor++)
  33. minimoSencillo(valor);
  34. cout << "Ingrese el valor a sencillar: "; cin >> valor;
  35. cout << "La cantidad de monedas para ese valor es: " << valores[valor].cant << endl;
  36. cout << "Las monedas son: ";
  37. while (valor > 0) {
  38. cout << valores[valor].moneda << ",";
  39. valor -= valores[valor].moneda;
  40. }
  41. cout << "\b." << endl;
  42. system("pause>0");
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement