marcelofoxes

1018 Banknotes URIJudge

May 15th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int availableNotes[] =  {100, 50, 20, 10, 5, 2, 1};
  4. int availableNotesLen = sizeof(availableNotes) / sizeof(int);
  5.  
  6. void decompose(int rawValue, int noteIndex) {
  7.  
  8.     if (noteIndex >= availableNotesLen) {
  9.         return;
  10.     }
  11.  
  12.     // get the current note value from our array
  13.     int noteValue = availableNotes[noteIndex];
  14.  
  15.     int notesCount = rawValue / noteValue;
  16.     int excedent = rawValue % noteValue;
  17.  
  18.     printf("%d nota(s) de R$ %d,00\n", notesCount, noteValue);
  19.  
  20.     // recalc with a lower note
  21.     decompose(excedent, ++noteIndex);
  22. }
  23.  
  24. int main(int argc, char const *argv[])
  25. {
  26.     int value;
  27.     scanf("%d", &value);
  28.  
  29.     printf("%d\n", value);
  30.  
  31.     // decompose value, starting with the first note
  32.     decompose(value, 0);
  33. }
Add Comment
Please, Sign In to add comment