如何执行PHP乘法逐步

时间:2015-12-23 19:22:50

标签: php

我有一个数组,我想乘以106乘以76但我想在这个模式中乘以它没有给出正确的结果

              1 0 6
               6 7 
      _____________
             7 4 2
           6 3 6 x 
   _________________ 
           7 1 0 2

代码运行正常但是添加错误我想代码的输出是424991它应该是7102

     <?php


    $data=array(
    'digit1' => 1,
    'digit2' => 0,
    'digit3' => 6,
    'digit4' => 6,
    'digit5' => 7 );

        $a=$data['digit1']*$data['digit4'];
        $b=$data['digit2']*$data['digit4'];
        $b1=$data['digit3']*$data['digit4'];
        $c=$data['digit1']*$data['digit5'];
        $d=$data['digit2']*$data['digit5'];
        $d1=$data['digit3']*$data['digit5'];
        $e=$b+$a+$b1;
        echo $e;
        $f=$c+$d+$d1;
        echo $f;

        echo $e+$f;




?>

2 个答案:

答案 0 :(得分:1)

使用floormodulus,您应该可以解决问题。

尝试this updated code

$data = array(
    'digit1' => 1,
    'digit2' => 0,
    'digit3' => 6,
    'digit4' => 6,
    'digit5' => 7
);

# ------------------------------------------------------------
$product = $data['digit5'] * $data['digit3'];
$a = $product % 10;
$carry = floor($product / 10);

$product = ($data['digit5'] * $data['digit2']) + $carry;
$b = $product % 10;
$carry = floor($product / 10);

$product = ($data['digit5'] * $data['digit1']) + $carry;
$c = $product;
#-------------------------------------------------------------
$product = $data['digit4'] * $data['digit3'];
$d = $product % 10;
$carry = floor($product / 10);

$product = ($data['digit4'] * $data['digit2']) + $carry;
$e = $product % 10;
$carry = floor($product / 10);

$product = ($data['digit4'] * $data['digit1']) + $carry;
$f = $product;
#-------------------------------------------------------------
$x = intval($c . $b . $a);
$y = intval($f . $e . $d) * 10;
#-------------------------------------------------------------

echo sprintf("%' 10d\n", $c.$b.$a);
echo sprintf("%' 9d\n", $f.$e.$d);
echo "----------\n";
echo sprintf("%' 10d\n", $x + $y);

您可以稍后感谢我为您完成作业。

答案 1 :(得分:0)

在您的计划中,您没有考虑到地方价值。

例如,当您将$data['digit4']乘以echo时,您应该将100乘以60,而不是将1乘以6。

你有一个更大的数字(6位数)的原因是你$data = array( 'digit1' => 1, 'digit2' => 0, 'digit3' => 6, 'digit4' => 6, 'digit5' => 7 ); $a=$data['digit1']*$data['digit4']; // $a = 6 $b=$data['digit2']*$data['digit4']; // $b = 0 $b1=$data['digit3']*$data['digit4']; // $b1 = 36 $c=$data['digit1']*$data['digit5']; // $c = 7 $d=$data['digit2']*$data['digit5']; // $d = 0 $d1=$data['digit3']*$data['digit5']; // $d1 = 42 $e=$b+$a+$b1; // $e = 42 echo $e; // OUTPUT 42 $f=$c+$d+$d1; // $f = 49 echo $f; // OUTPUT 42 echo $e+$f; // OUTPUT 91 两个值。

这是你的程序正在做的事情(一步一步):

$data = array(
    'digit1' => 1,
    'digit2' => 0,
    'digit3' => 6,
    'digit4' => 6,
    'digit5' => 7
);

$a=$data['digit1']*100*$data['digit4']*10;
$b=$data['digit2']*10*$data['digit4']*10;
$b1=$data['digit3']*1*$data['digit4']*10;
$c=$data['digit1']*100*$data['digit5']*1;
$d=$data['digit2']*10*$data['digit5']*1;
$d1=$data['digit3']*1*$data['digit5']*1;
$e=$b+$a+$b1;
$f=$c+$d+$d1;
echo $e+$f;

这是创建输出424991.

如果您想让您的程序按原样运行(而不是使用如下所示的动态程序),那么以下内容应该有效:

$num1 = "106";
$num2 = "67";

您的代码有点过于严格,我可以建议更动态的版本吗?

给出PHP中的任何两个数字字符串:

$num1Split = str_split($num1);
$num2Split = str_split($num2);

您可以将每个分成数组:

$total = 0;
for($i = 0; $i < count($num1Split); $i++)
    for($j = 0; $j < count($num2Split); $j++)
        $total += $num1Split[$i]*pow(10,count($num1Split)-$i-1) * $num2Split[$j]*pow(10,count($num2Split)-$j-1);
echo $total;

然后将其相乘:

{{1}}