Advertisement
Spocoman

Food For Pets

Feb 22nd, 2022 (edited)
153
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function foodForPets(input) {
  2.     let days = Number(input[0]);
  3.     let food = Number(input[1]);
  4.     let dogsFood = 0;
  5.     let catsFood = 0;
  6.     let cookies = 0;
  7.     let dayCount = 1;
  8.    
  9.     for (let i = 2; i <= days * 2; i += 2) {
  10.         let dayFoodForDog = Number(input[i]);
  11.         let dayFoodForCat = Number(input[i + 1]);
  12.  
  13.         if (dayCount % 3 === 0) {
  14.             cookies += (dayFoodForDog + dayFoodForCat) / 10;
  15.         }
  16.         dogsFood += dayFoodForDog;
  17.         catsFood += dayFoodForCat;
  18.         dayCount++;
  19.     }
  20.  
  21.     let totalFood = dogsFood + catsFood;
  22.     console.log(`Total eaten biscuits: ${Math.round(cookies)}gr.`);
  23.     console.log(`${(totalFood / food * 100).toFixed(2)}% of the food has been eaten.`);
  24.     console.log(`${(dogsFood / totalFood * 100).toFixed(2)}% eaten from the dog.`);
  25.     console.log(`${(catsFood / totalFood * 100).toFixed(2)}% eaten from the cat.`);
  26. }
  27.  
  28. РЕШЕНИЕ СЪС SHIFT():
  29.  
  30. function foodForPets(input) {
  31.     let days = Number(input.shift());
  32.     let food = Number(input.shift());
  33.     let dogsFood = 0;
  34.     let catsFood = 0;
  35.     let cookies = 0;
  36.    
  37.     for (let i = 1; i <= days; i++) {
  38.         let dayFoodForDog = Number(input.shift());
  39.         let dayFoodForCat = Number(input.shift());
  40.  
  41.         if (i % 3 === 0) {
  42.             cookies += (dayFoodForDog + dayFoodForCat) / 10;
  43.         }
  44.         dogsFood += dayFoodForDog;
  45.         catsFood += dayFoodForCat;
  46.     }
  47.  
  48.     let totalFood = dogsFood + catsFood;
  49.     console.log(`Total eaten biscuits: ${Math.round(cookies)}gr.`);
  50.     console.log(`${(totalFood / food * 100).toFixed(2)}% of the food has been eaten.`);
  51.     console.log(`${(dogsFood / totalFood * 100).toFixed(2)}% eaten from the dog.`);
  52.     console.log(`${(catsFood / totalFood * 100).toFixed(2)}% eaten from the cat.`);
  53. }
  54.  
  55. РЕШЕНИЕ С WHILE И SHIFT():
  56.  
  57. function foodForPets(input) {
  58.     let days = Number(input.shift());
  59.     let food = Number(input.shift());
  60.     let dogsFood = 0;
  61.     let catsFood = 0;
  62.     let cookies = 0;
  63.     let dayCount = 1;
  64.    
  65.     while (days-- !== 0) {
  66.         let dayFoodForDog = Number(input.shift());
  67.         let dayFoodForCat = Number(input.shift());
  68.  
  69.         if (dayCount++ % 3 === 0) {
  70.             cookies += (dayFoodForDog + dayFoodForCat) / 10;
  71.         }
  72.         dogsFood += dayFoodForDog;
  73.         catsFood += dayFoodForCat;
  74.     }
  75.  
  76.     let totalFood = dogsFood + catsFood;
  77.     console.log(`Total eaten biscuits: ${Math.round(cookies)}gr.`);
  78.     console.log(`${(totalFood / food * 100).toFixed(2)}% of the food has been eaten.`);
  79.     console.log(`${(dogsFood / totalFood * 100).toFixed(2)}% eaten from the dog.`);
  80.     console.log(`${(catsFood / totalFood * 100).toFixed(2)}% eaten from the cat.`);  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement