本文介紹了如何乘數直到達到個位數并計算數字?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
它的描述是這樣的:
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];
}
接下來我就糊涂了
有什么解決方案可以解決我的問題?
Any solution to solve my problem?
推薦答案
$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<count($array);$i++){ //Iterate through all permutations
$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
這篇關于如何乘數直到達到個位數并計算數字?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!