2016-07-16 124 views
0

方案如何确保一个整数整除另一个整数

我有一个房间预订单。

以下所有领域都是整数

roomsneeded(总工室需要。基于maxguest计算)

maxguest(每间客房允许的最大客)

checkin_guest_count(总人数需要房间)

所以作为maxguest increases/decreases我需要increase/decreaseroomsneeded

所以在考虑到上述保持让您的example

比方说maxguest=3checkin_guest_count=17 ..所以我怎么能计算roomsneeded

modulus有用,因为我不是在寻找exact divisibles

我试图做到这一点的javascript(但寻找的基本 思想对数学需要)

+1

'function roomsneeded(maxguest,checkin_guest_count){return Math.ceil(checkin_guest_count/maxguest)}'? – michaelsnowden

回答

2

而不是检查,该数字是整除 - 假设你可以有比乘员在一间客房的最大数量较少 - 只需使用Math.ceil()

function calculate() { 
 

 
    // retrieving the various values required for the 
 
    // calculation: 
 
    var checkin_guest_count = document.getElementById('checkin_guest_count').value, 
 
    maxguest = document.getElementById('maxguest').value, 
 
    result = document.getElementById('result'), 
 

 
    // dividing the number of guests by the maximum 
 
    // number of guests per room, and then rounding 
 
    // that value up to the nearest integer (eg 
 
    // Math.ceil(5.1) will return 6): 
 
    numRooms = Math.ceil(checkin_guest_count/maxguest); 
 

 
    // here we update the value of the 'result' node 
 
    // to either the numRooms value (if numRooms is a 
 
    // finite number, so we haven't tried to divide by 
 
    // zero) or to 0 if the numRooms is infinite: 
 
    result.value = Number.isFinite(numRooms) ? numRooms : 0; 
 
    return numRooms; 
 
} 
 

 
// retrieving the collection of <input> elements whose 
 
// type attribute is 'number', and converting that collection 
 
// to an Array (using Array.from()): 
 
var inputs = Array.from(document.querySelectorAll('input[type = number]')); 
 

 
// iterating over the Array using Array.prototype.forEach(): 
 
inputs.forEach(function(input) { 
 

 
    // binding the named function calcuate() (note the lack of 
 
    // parentheses in the event-binding) as the event-handler 
 
    // for the 'change' event: 
 
    input.addEventListener('change', calculate); 
 
});
label { 
 
    display: block; 
 
    margin: 0 0 0.2em 0; 
 
} 
 
label span { 
 
    display: inline-block; 
 
    width: 50%; 
 
    text-align: right; 
 
} 
 
label span::after { 
 
    content: ': '; 
 
} 
 
label span + input { 
 
    width: 20%; 
 
}
<label><span>Number of guests</span> 
 
    <input type="number" id="checkin_guest_count" step="1" /> 
 
</label> 
 

 
<label><span>Maximum guests per room</span> 
 
    <input type="number" id="maxguest" step="1" min="1" /> 
 
</label> 
 

 
<label><span>Number of rooms</span> 
 
    <input type="number" id="result" readonly /> 
 
</label>
:下面概念的

var roomsNeeded = Math.ceil(checkin_guest_count/maxguest); 
// 17/3 => 5.6... 
// Math.ceil(17/3) => 6 

证明

参考文献:

+0

谢谢您的详细信息.. – Abhilash

+1

您的确非常欢迎,我很高兴能有一些帮助! :) –

1

你可以做这样的事情:

roomsneeded = Math.cell(checkin_guest_count/maxguest); 

数学.ceil()四舍五入至最接近的整数

1

我试图做到这一点的JavaScript(但寻找所需要数学的基本 想法)

所以基本上你的房间需要的永远是:

var rooms_needed = Math.ceil(checkin_guest_count/maxguest); 

如果客人超过最大容量,应该给另一个房间。

0

整数除法应该给你17/3 = 5。那么如果17 mod 3大于0,则需要一个房间(5 + 1)。

+2

欢迎来到StackOverflow。对于代码示例,您可以使用代码块来封装代码。对提问者和其他读者来说这会更有帮助。 –

1

我会做这样的事情:

if(!(int1 % int2)) { 
/* Your cool code here */ 
} 

你不需要别的

0

您可以通过另一个使用这种方案是否一个整数整除喜欢他们想要为他们自己提供个人房间,而他们可以与其他人分享房间不计算,那么你可以填充每个房间的最大数量的人可以并且这意味着您每间客房允许的最大客人数乘以房间总数应该等于客人总数。

rooms * max_guests = total_guests

这意味着

rooms = total_guests/max_guests

自此房数不能是分数,你将不得不采取的是天花板。

如果每个客人都可以像他们想单独呆在一起,那么你必须采取最坏的情况,这意味着房间总数等于客人人数。

rooms = total_guests

注:模数师提供了后去渣,你可以使用模数指明是否加1,分割后保留在方法1

也就是说,如果total_guests % max_guests != 0商再加入1到总房间。

0

如果人们:

var maxguest = 3; //magic number? 
var checkin_guest_count = 17; //magic number? 

function roomsneeded(maxGuest, checkinGuestCount) { 
    if (maxGuest > 0) { // Dividing by zero creates black holes 
     return Math.ceil(checkinGuestCount/maxGuest); //Math.ceil => round to highest int 
    } else return 0; 
} 
alert(roomsneeded(maxguest, checkin_guest_count)); 
相关问题