使用Modulo来计算余数

    //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盛宴将会有多少片? 示例数据集:10人,比萨饼和每片披萨8片意味着每个人吃3片,而斯帕奇得到2片。 (请注意,这是一个例子,无论我给那些给定变量输入什么数字,你的代码都应该工作并给我准确的结果。)给定:不要为此给出新的给定变量/常量。 取而代之的是使用你为馅饼I片设置的食物。结果变量:斯帕基得到的片数。 打印结果:“Sparky得到了X片披萨。”

我已经插入并尝试尽可能多的方法,但我可以用它来处理不同的数字。 它总是一点点关闭。


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

每个客人都得到slicesPerPerson,而Sparky获得剩菜。

链接地址: http://www.djcxy.com/p/58289.html

上一篇: Using Modulo to calculate remainder

下一篇: Modulo operation with negative numbers