【问题标题】:Divide integer number into 3 parts using php [closed]使用php将整数分成3部分[关闭]
【发布时间】:2013-09-04 09:45:04
【问题描述】:

我需要使用 php 将一个整数值分成 3 部分

例如:我有一个值 9 答案就像 3 + 3 + 3 如果我的值为 10,则 ans 像 3 + 3 + 4 或类似的东西

【问题讨论】:

  • 如果你的值是2
  • @491243 可能是0+1+1
  • 如果值为 7? 3+3+1?所以总是包含3?解释更多
  • 10 = 2+4+4 , 10 = 3+5+2,10=1+8+1 ....so on.. 你想要哪三个数字??
  • “或类似的东西”,那么“鱼、猫、青蛙”可以接受吗?

标签: php integer divide


【解决方案1】:

您可以使用模数函数。

<?php
    $yourInt=10;
    $remainder=$yourInt % 3;
    $third=floor($yourInt/3);
    $lastBit=$third+$remainder;

    echo "The numbers are $third + $third + $lastBit.";
?>

输出:

The numbers are 3 + 3 + 4.

【讨论】:

  • 可以想到模运算符 :D
  • 没问题,如果你对它感到满意,接受答案是礼貌的,这样其他人就不会花时间回答你已经满意的东西了:)
  • @Fluffeh 我认为您的回答无效。你试过了吗?
  • @Simon_eQ 不,只是掷骰子。但是,我确实在测试时修复了一些错别字以及现在如何更新:)
【解决方案2】:

如果您的值 >= 3,您可以执行以下操作:

$number = 10;
$number1and2 = floor($number/3);
$number3 = $number - (2*$number1and2);

【讨论】:

    【解决方案3】:
    $num = 11; 
    
    $d = [$num/3,$num/3,$num/3];
    
    $round = array_map('round', $d);    
    list($first, $second, $third) = $round;
    $round = (is_float($num/3)) ? $num-(round($num/3)*3) : 0;
    
    echo $first.' '.$second.' '.($third += (is_float($num/3)) ? $num-(round($num/3)*3) : 0); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      相关资源
      最近更新 更多