簡體   English   中英

使用模數計算余數

[英]Using Modulo to calculate remainder

    //How much pizza each party goer will recieve
    const slices = 3; //Number of slices 8
    var people = 8; //Number of people 25
    var pizzas = 10; //Number of pizzas 10

   //pizzas ordered times the number of slices, divided by the number of people & asign         slicePerson variable
   var slicePerson = (slices * pizzas)/people;
  //print out how many pieces per person
  console.log("Each person ate" + " " + slicePerson + " " + "slices of pizza at the party.")



  //Sparky gets what remainder of pizza
  //Slices by pizzas used modulo to give remainder with people
  var dogFood = 30 % people;
  //print out how many slices sparky got
 console.log("Sparky got" + " " + dogFood + " " + "slices of pizza.");

問題:

在披薩派對Sparky上,主人的狗很興奮,因為主人將切片均勻地分成客人后得到了剩下的披薩。 假設客人得到了整塊,Sparky盛宴要鋪幾塊? 示例數據集:10個人,4個比薩餅和每個比薩餅8片將意味着每個人吃3片,Sparky得到2片。 (請注意,這是一個示例,無論我為這些給定變量輸入多少數字,您的代碼都應該起作用並給我准確的結果。)給定:請勿為此創建新的給定變量/常量。 取而代之的是使用您為Pie I切片設置的給定值。結果變量:Sparky可以食用的切片數。 打印結果:“ Sparky得到了X片披薩。”

我已經插入並嘗試了盡可能多的方法,但是我可以將其用於不同的數字。 它總是一點點偏離。

var leftovers = (slicesPerPizza * numberOfPizzas) % numberOfGuests;
var slicesPerPerson = (slicesPerPizza * numberOfPizzas - leftovers) / numberOfGuests;

每個來賓獲得slicesPerPerson,Sparky獲得剩菜剩飯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM