我们有三个变量输入
<html>
<body>
<form method="post">
<input name="a">
<input name="c">
<input name="f">
</form>
</body>
</html>
okey,数学代码是解决此问题的方法。 a等于:c等于年数,例如10:f到25
所以a是200,c是10,f是25这些数字是我们的输入
所以数学对此做出了答案
x1= a*f/100
x2 = ((a-x1)*f)/100
x3 = ((a-x1-x2)*f)/100
x4 = ((a-x1-x2-x3)*f)/100
x5 = ((a-x1-x2-x3-x4)*f)/100
x6 = ((a-x1-x2-x3-x4-x5)*f)/100
x7 = ((a-x1-x2-x3-x4-x5-x6)*f)/100
x8 = ((a-x1-x2-x3-x4-x5-x6-x7)*f)/100
x9 = ((a-x1-x2-x3-x4-x5-x6-x7-x8)*f)/100
x10 = ((a-x1-x2-x3-x4-x5-x6-x7-x8-x9)*f)/100
请帮助编写php代码。我用这个for循环
<?php
$a = $_POST['a'];
$c = $_POST['c'];
$f = $_POST['f'];
for ($i=1;$i<=$c;$i++){
$x = $a*f/100;
$x[$i] = ($a (-$x[$i][])*f)/100;
echo $x[$i];
echo '<br>';
}
?>
答案 0 :(得分:2)
我会尝试将其循环遍历并将结果存储在数组中,在这种情况下,将其存储在$x
中。
echo
只是为了说明逻辑,您可以删除它们。
<?php
$a = 200;
$c = 10;
$f = 25;
$x[] = $a*$f/100; //Assign first value, so $x[0] = 50
for ($i=0;$i<$c;$i++){ //$i < $c because array first index is 0
$xIn = $a;
echo 'x' . ($i+1) . ' = ((a'; //$i + 1 is just for printing.
for ($i2=0;$i2 < $i;$i2++){
echo '-x' . ($i2+1); //$i2 + 1 is just for printing.
$xIn -= $x[$i2];
}
echo ')*f)/100';
$x[$i] = ($xIn * $f) / 100; //Total.
echo ' | Total = ' . $x[$i] . '<br>';
}
?>
您可以在此处粘贴代码并测试:http://phptester.net/
答案 1 :(得分:1)
这是要求输出的最简单方法
<?php
$a = $_POST['a'];
$c = $_POST['c'];
$f = $_POST['f'];
$result = 0;
for($i=0; $i<$c; $i++){
$x = (($a-$result)*$f)/100;
$result = $result + $x;
echo $x."<br>";
}
答案 2 :(得分:0)
请在下面尝试。
<?php
$a = 200;
$c = 10;
$f = 25;
$values = [];
echo '<pre>';
for ($i = 1; $i <= $c; $i++) {
$diff = $a;
foreach ($values as $value) {
$diff -= $value;
}
$values['x' . $i] = ($diff * $f) / 100;
}
print_r($values);
?>
答案 3 :(得分:0)
这是解决您的问题所需的最低代码...
<?php
$a = $_POST['a'];
$c = $_POST['c'];
$f = $_POST['f'];
$x = [];
$x[0] = $a*$f/100;
$logic = $a;
for ($i=1;$i<count($c);$i++){
for ($j=$i-1;$j >= $i;$j--){
$logic -= $x[$j];
}
$x[$i] = ($logic*f)/100;
echo $x[$i];
echo '<br>';
}
?>