如何计算如何应用家庭票折扣,然后按相关费用收取所有剩余的门票(即成人门票费用超过儿童门票)?
这是我到目前为止所做的(似乎有效,但对它没有100%的信心(php))
# Work out how many pairs of people there are
$numAdultPairs = floor($oForm->adult / 2);
$numChildPairs = floor($oForm->child / 2);
# work out the number of matching pairs for child / adult
if ( $numAdultPairs > $numChildPairs ) {
$numberOfFamilyTickets = $numAdultPairs - $numChildPairs;
} else if ( $numAdultPairs < $numChildPairs ){
$numberOfFamilyTickets = $numChildPairs - $numAdultPairs;
} else if ( $numAdultPairs == $numChildPairs ) {
$numberOfFamilyTickets = $numAdultPairs;
}
# work out the remaining tickets required
$remainingAdult = $oForm->adult % 2;
$remainingChild = $oForm->child % 2;
答案 0 :(得分:2)
沿着这些方向的东西(伪代码):
adultPairs = totalAdults / 2
childPairs = totalChildren / 2
familyPairs = min(adiltPairs, childPairs)
adults = totalAdults - familyPairs * 2
children = totalChildren - familyPairs * 2
答案 1 :(得分:2)
我不认为你的方法确实有效。假设您有6名成人和4名儿童。在这种情况下,您的第一个if
将成立,最终会获得3 - 2 = 1
家庭票。不过,正确的结果将是两张家庭票和两张成人票。
您希望最少的对数来确定家庭门票的数量。尝试这样的事情:
$numberOfFamilyTickets = min($numAdultPairs, $numChildPairs);
$remainingAdult = $oForm->adult - 2 * $numberOfFamilyTickets;
$remainingChild = $oForm->child - 2 * $numberOfFamilyTickets;
假设您确保$oForm->adult
和$oForm->child
是非负面的。
答案 2 :(得分:1)
这样的事也许......
minfamilies = min(NumberOfAdults,NumberOfChildren)/ 2
RemaningAdults = NumberOfAdults - (minfamilies * 2)
RemainingChildren = NumberOfChildren - (minfamilies * 2)
编辑:冗余评论。以上两条评论都很合理,我只是跳进去了:D