一些两个值并添加到下一个值金字塔计算



我需要一些解决方案在PHP或python计算金字塔形式需要循环,直到两位数的值仍然

我现在这个形式不是为代码创建,但我尝试了很多东西,但没有找到我的问题的解决方案

这里是输入

12345

需要这样的输出:add value to next value remove (0)

12345
3579
837
21

解释:现在我解释输出公式
1 + 2 and 2 + 3 and 3 + 4 and 4 + 5= 3579

第二行是3579

3 + 5 = [8]  = 5 + 7 = (12) and add 1 + 2 = [3] and 7 + 9 = 16 and 1 + 6 = [7] = 837

3日行837

8 + 3 = (11) = 1 + 1 = [2] and 3 + 7 = (10) 1 + 0 = [1] = 21

检查$value == $string1是否在打印之前而不是之后,因此您不打印匹配的值。

<?php 

if(isset($_REQUEST['submit']))
{
$string = preg_replace('/s+/', '', $_REQUEST['convert']);          
$string1 = preg_replace('/s+/', '', $_REQUEST['convert']);
echo $string;
echo "<br>";
$formatted = implode(' ',str_split($string));
echo $formatted;
echo "<br>";

$count = mb_strlen($string);

for($loop=0;$loop<$count;$loop++)
{               
$value='';          
$j=$count-1;
$total=$count/2;

for($i=0;$i<$total;$i++)
{
$value .=$string[$j];
$value .=$string[$i];
$j--;
}
if ($value == $string1) {
exit();
}
if($count%2==0)
{
$value;
$formatted = implode(' ',str_split($value));
echo $formatted;
}
else{
$value=substr_replace($value, "", -1);
$formatted = implode(' ',str_split($value));
echo $formatted;
}
echo "<br>";
$string=$value;         
}           
}

?>

最新更新