dro59505 2009-02-02 09:21
浏览 47
已采纳

计算2x成人+ 2x儿童组合的数量

  • a ticketing site offers a discount on "family" tickets.
  • a family ticket is 2 adults, 2 children.
  • the sell tickets page allows a user to enter any number of adult and child tickets.

How do I work out how to apply the family ticket discount, then charge all remaining tickets at their relevant cost (i.e. adult tickets cost more than child tickets) ?

Here's what I have so far (seems to work, but not 100% confident about it (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;
  • 写回答

3条回答 默认 最新

  • dtrn74832 2009-02-02 09:35
    关注

    I don't think your approach actually works. Assume you have 6 adults and 4 children. In this case your first if would be true and you would end up with 3 - 2 = 1 family ticket. The correct result would be two family tickets and two adult tickets, though.

    You want the lowest number of pairs to determine the amount of family tickets. Try something like this:

    $numberOfFamilyTickets = min($numAdultPairs, $numChildPairs);
    $remainingAdult = $oForm->adult - 2 * $numberOfFamilyTickets;
    $remainingChild = $oForm->child - 2 * $numberOfFamilyTickets;
    

    Assuming you make sure that $oForm->adult and $oForm->child are non-negative.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭