Advertisement
Guest User

Untitled

a guest
Sep 15th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. /*
  2.  * File:   Coin Row Problem
  3.  * Author: Fernando Alva Manchego
  4.  *
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. /*
  11.  *
  12.  */
  13.  
  14. #define MAX_MONEDAS 6
  15.  
  16. int max(int val1, int val2){
  17.     if (val1>val2)
  18.         return val1;
  19.     else
  20.         return val2;
  21. }
  22.  
  23. int coin_row(int cantidades[], int monedas[]){
  24.     int i;
  25.    
  26.     cantidades[0] = 0;
  27.     cantidades[1] = monedas[0];
  28.    
  29.     for (i=2; i<=MAX_MONEDAS; i++){
  30.         cantidades[i] = max(monedas[i-1] + cantidades[i-2], cantidades[i-1]);
  31.     }
  32.    
  33.     return cantidades[MAX_MONEDAS];
  34. }
  35.  
  36. void pprint(int cantidades[]){
  37.     int i;
  38.     for (i=0; i<=MAX_MONEDAS;i++)
  39.         printf("%d ", cantidades[i]);
  40.    
  41. }
  42.  
  43. int main(int argc, char** argv) {
  44.    
  45.     int monedas[MAX_MONEDAS] = {5,1,2,10,6,2};
  46.     int cantidades[MAX_MONEDAS+1];
  47.    
  48.     printf("%d\n", coin_row(cantidades,monedas));
  49.    
  50.     printf("Estructura Auxiliar:\n");
  51.     pprint(cantidades);
  52.    
  53.     return (EXIT_SUCCESS);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement