Advertisement
dimipan80

Exams - Cakes

Dec 27th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* AniG owns S leva and wants to spend as much of them as she can. She also has 3 favorite types of cakes.
  2.  The first one costs C1 leva, the second one C2 leva and the third one C3 leva.
  3.  In the shop there are infinite number of cakes but AniG has only S leva.
  4.  AniG should spend as much as she can of his money to buy some cakes.
  5.  Find the maximum amount of money (no more than S) that she can spend to buy cakes.
  6.  On the first line there will be the number S.
  7.  On the second, third and fourth line there will be the numbers C1, C2 and C3.
  8.  Output the biggest possible amount that AniG can spend. */
  9.  
  10. "use strict";
  11.  
  12. function solve(args) {
  13.     var s = parseInt(args[0]);
  14.     var c1 = parseInt(args[1]);
  15.     var c2 = parseInt(args[2]);
  16.     var c3 = parseInt(args[3]);
  17.  
  18.     var amounts = [];
  19.     var i, j, k, amount;
  20.     for (i = 0; i < 100; i += 1) {
  21.         for (j = 0; j < 100; j += 1) {
  22.             for (k = 0; k < 100; k += 1) {
  23.                 amount = c1 * i + c2 * j + c3 * k;
  24.                 if (amount <= s) {
  25.                     amounts.push(amount);
  26.                 }
  27.             }
  28.         }
  29.     }
  30.  
  31.     amounts.sort(function (a, b) {
  32.         return b - a
  33.     });
  34.    
  35.     console.log(amounts[0]);
  36. }
  37.  
  38. solve(['110', '13', '15', '17']);
  39. solve(['20', '11', '200', '300']);
  40. solve(['110', '19', '29', '39']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement