armstrongnumber.htm

来自「“常见程式演算”主要收集一些常见的程式练习题目」· HTM 代码 · 共 117 行

HTM
117 行
字号
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>




  
  
  
  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">




  
  
  
  
  <link rel="stylesheet" href="css/print.css" type="text/css">




  
  
  
  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">




  
  
  
  
  <title>阿姆斯壮数</title>
</head>


<body>




<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>




<h1><a href="AlgorithmGossip.htm">Algorithm Gossip: 阿姆斯壮数</a></h1>




<h2>说明</h2>

在三位的整数中,例如153可以满足1<sup>3</sup> + 5<sup>3</sup> + 3<sup>3</sup> = 153,这样的数称之为Armstrong数,试写出一程式找出所有的三位数Armstrong数。<br>

<h2>解法</h2>

Armstrong数的寻找,其实就是在问如何将一个数字分解为个位数、十位数、百位数......,这只要使用除法与余数运算就可以了,例如输入 input为abc,则:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">a = input / 100 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">b = (input%100) / 10 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">c = input % 10 </span><br>

</div>







<h2> 实作</h2>


<ul>

  <li> C
  </li>

</ul>


<pre>#include &lt;stdio.h&gt; <br>#include &lt;time.h&gt; <br>#include &lt;math.h&gt; <br><br>int main(void) { <br>    int a, b, c; <br>    int input; <br><br>    printf("寻找Armstrong数:\n"); <br><br>    for(input = 100; input &lt;= 999; input++) { <br>        a = input / 100; <br>        b = (input % 100) / 10; <br>        c = input % 10; <br>        if(a*a*a + b*b*b + c*c*c == input) <br>            printf("%d ", input); <br>    } <br><br>    printf("\n"); <br><br>    return 0; <br>} <br></pre>


<br>


<ul>

  <li> Java
  </li>

</ul>


<pre>public class Armstrong {<br>    public static void main(String[] args) {<br>        System.out.println("寻找Armstrong数:"); <br><br>        for(int i = 100; i &lt;= 999; i++) { <br>                int a = i / 100; <br>                int b   = (i % 100) / 10; <br>                int c = i % 10; <br>                if(a*a*a + b*b*b + c*c*c == i) <br>                        System.out.print(i + " "); <br>        } <br><br>        System.out.println();<br>    }<br>}</pre>

<br>

<br>




</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?