2011-12-19 40 views
0

我只是一個初學者在PHP中。我試圖爲阿姆斯特朗數字編寫代碼。我已在153.我的代碼檢查。我的代碼的輸出是0.153不是一個阿姆斯特朗數。我可以糾正它?這是我的代碼。在阿姆斯特朗代碼中的錯誤

<?php 
$n=153; 
while($n>1) 
{ 
    $b=$n%10; 
    $c=$b*$b*$b; 
    $n=$n/10; 
    $d=$c+$d; 
} 
if($d==$n) 
    echo $n."is an armstrong number"; 
else 
    echo $n."is not an armstrong number"; 
?> 
+0

PHP沒有使用整數結果實現整數除法,而忽略了C的提示。 '$ n = $ n/10;'產生一個浮點值,並且從那一點起代碼就不存在了。 – axiac 2018-02-08 10:23:46

回答

1

你正在改變你的$n循環內,並用它進行比較($d==$n)和輸出之後。複製$n的值並在循環中使用它。

備註:您的循環應測試$n>0而不是$n>1

+0

謝謝。現在工作正常。 – designersvsoft 2011-12-19 10:14:23

0
<?php 
$n = 153; 
$tempN = $n; 

while($tempN > 0) { 
    $remainder = $tempN % 10; 
    $sum = $sum + $remainder * $remainder * $remainder; 
    $tempN = $tempN/10; 
} 

if($n == $sum) { 
    /* armstrong number */ 
} else { 
    /* not armstrong number */ 
} 
?> 
+0

此代碼遭受與原始代碼相同的錯誤。請看我的答案。 – Howard 2011-12-19 09:36:03

+0

噢,是的,固定.. – 2011-12-19 09:37:40

+0

在哪裏我可以看到答案? – designersvsoft 2011-12-19 09:45:28

2

鑑於Armstrong Numbers definition,功能性解決方案更簡潔一點更清楚我

function isArmstrongNumber($number) { 
    $digits=str_split($number); // create an array containing the digit into the 
    $result = array_sum(
     array_map(
      function($x){return $x*$x*$x;}, 
      $digit 
     ) 
    ); 
    return $number == $result; 
} 

事實證明,以前的定義是有效的三個數字。 一個general definition導致了 略有不同的功能:

function isArmstrongNumber($number) { 
    $digits=str_split($number); // create an array containing the single digits 
    $power= count($digits);  // the power every digit has to be raised to 
    $result = array_sum(
     array_map('pow', $digits, array_fill(0,$power,$power)) 
    ); 
    return $number == $result; 
} 

您可以使用代碼以這樣的方式

$candidate = 153; 
if(isArmstrongNumber($candidate) { 
    /* armstrong number */ 
    echo $candidate, ' is an Armstrong number.'; 
} else { 
    /* not armstrong number */ 
    echo $candidate, ' is not an Armstrong number.'; 
} 
0
$num = 371; 
$sum = 0; 
$temp = $num; 
while($temp!= 0){ 
    $rem = $temp%10; 
    $sum = $sum+($rem*$rem*$rem); 
    $temp = $temp/10; 

} 
if($num == $sum){ 
    echo "Armstrong number"; 
} 
else{ 
    echo "not an armstrong number"; 
} 
-1

您可以檢查是否有數量是阿姆斯特朗還是不

<?php 
if(isset($_POST['log'])) 
{ 
    echo $limit=$_POST['limit']; 
    amstrong_checker($limit); 
} 
function amstrong_checker($val){// $val take value from user. 
    $value=$val; 
    $counter=0; 
    $amstrong=0; 
    while($val >= 1){ 
    $digit[$counter]=$val%10; 
    $val = $val/10; 
    $counter=$counter+1; //count the digits of a value. 
    } 
    for($i=0 ; $i<$counter ; $i++){ 
    $data=1; 
    for($j=0 ; $j<$counter ; $j++){ 
     $data=$data * $digit[$i]; //calculate according to digits count. 
    } 
    $amstrong=$amstrong+$data;//calculate sum of total. 
    } 
    if($amstrong==$value){ //checking. 
    echo 'amstrong number'; 
    } 
    else{ 
    echo 'not a amstrong number'; 
    } 
} 
?> 

<!doctype html> 
<html> 
<body> 
<body> 
<form method="post" > 
<table border="0"> 
    <tbody> 
    <tr> 
     <td>limit</td> 
     <td><input type="text" name="limit"></td> 
    </tr> 
    <tr> 
     <td><input name="log" type="submit" value="submit"></td> 
    </tr> 
    </tbody> 
</table> 

</body> 
</html> 
+0

歡迎來到Stack Overflow!雖然這段代碼可能會解決這個問題,但包含一個解釋確實有助於提高帖子的質量。請記住,你正在回答未來讀者的問題,而這些人可能不知道你的代碼建議的原因。 – ItamarG3 2018-02-08 10:05:17

+0

上面提到的代碼用於檢查任何數字是否爲amstrong。 – 2018-02-08 10:08:44

+0

你應該解釋你的代碼。在答案中。解釋它爲什麼起作用,或者如果它使用了一些衆所周知的方法,請提及這一點 – ItamarG3 2018-02-08 10:12:42

0

該pro你在問題中發佈的語法看起來像是從C中翻譯過來的。PHP提供了簡單的方法來處理字符串和數組;它們可以用來以數字分割數字並將變換應用於每個數字。

這是一個PHP的解決方案如何能夠樣子:

function isArmstrongNumber($number) 
{ 
    // The power is not always 3 but the number of digits of the number 
    $power = strlen($number); 

    // Compute the sum of values returned by array_map() 
    $sum = array_sum(
     array_map(
      // Apply this function to each digit 
      function ($digit) use ($power) { 
       // Compute $digit to the power of $power 
       return $digit ** $power; 
      }, 
      // Split the number into digits 
      str_split($number) 
     ) 
    ); 

    return $sum === $number; 
} 

exponentiator operator (**)可自PHP 5.6。對於舊版本,可以使用pow()函數。

相關問題