Advertisement
krock186

Untitled

Feb 15th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. int array[] = { 2, 3, 4, 7, 10 };
  2. int n = 18;
  3.  
  4. // Get smallest number
  5. int smallest_number = 999;
  6. for (int i = 0; i < length(array); i++)
  7.     if (array[i] < smallest_number)
  8.         smallest_number = array[i];
  9.  
  10. // If biggest number is at the end of your array - do it backwards:
  11. // Do it as long (n > 0)
  12. for (int i = length(array) - 1; i >= 0 && n > 0; i--) {
  13.     // Can subtract from n
  14.     if (array[index] > n)
  15.         continue;
  16.        
  17.     // Array value is smaller or equal 'n'
  18.     int new_value = n - array[index];
  19.     if (new_value > 0 && new_value < smallest_number)
  20.         continue;
  21.     else
  22.         n = new_value;
  23.  
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement