php两数相乘,php – 如何将数字相乘,直到达到一位数字并计算数字?
搜索熱詞
它的描述如下:
persistence(39) == 3 // because 3*9 = 27,2*7 = 14,1*4=4
// and 4 has only one digit
persistence(999) == 4 // because 9*9*9 = 729,7*2*9 = 126,// 1*2*6 = 12,and finally 1*2 = 2
persistence(4) == 0 // because 4 is already a one-digit number
我只能這樣做:
$array = str_split(39);
foreach ($array as $key => $value) {
echo $array[$key]*$array[$key+1];
}
接下來我很困惑
解決我的問題的任何解決方案?
$array = str_split('999'); //Your string
$j=0; //Counter for counting the number of iteration
while (count($array)>1){ //When more than 2 indexes in array
for($i=0;$i
$array = array_product($array); //Multiplies all numbers in array
$array = str_split($array); //Split the array up again
$j++; //Increment counter(as literal as I can sound)
}
}
echo $j; //Print out the number of times
總結
如果覺得編程之家網站內容還不錯,歡迎將編程之家網站推薦給程序員好友。
本圖文內容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。
總結
以上是生活随笔為你收集整理的php两数相乘,php – 如何将数字相乘,直到达到一位数字并计算数字?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML输入学生成绩并排序java_JS
- 下一篇: 动态规划算法php,php算法学习之动态