我需要使用php
将整数值分成3个部分例如:我有一个值9,答案就像是3 + 3 + 3 如果我有值10,那就像3 + 3 + 4或类似的东西
答案 0 :(得分:7)
您可以使用模数函数。
<?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.
答案 1 :(得分:2)
如果您的值>&gt; = 3,则可以执行以下操作:
$number = 10;
$number1and2 = floor($number/3);
$number3 = $number - (2*$number1and2);
答案 2 :(得分:1)
$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);