2016-07-16 122 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)); 
相關問題