目录

阿姆斯特朗号码(Armstrong Number)

阿姆斯特朗号码是一个等于其各个数字的立方总和的数字。 例如,153是一个阿姆斯特朗号 -

153 = (1)<sup>3</sup> + (5)<sup>3</sup> + (3)<sup>3</sup>
153 = 1 + 125 + 27
153 = 153

算法 (Algorithm)

这个程序的算法很简单 -

START
   Step 1 → Take integer variable Arms
   Step 2 → Assign value to the variable
   Step 3 → Split all digits of Arms
   Step 4 → Find cube-value of each digits
   Step 5 → Add all cube-values together
   Step 6 → Save the output to Sum variable
   Step 7 → If <u>Sum equals to Arms</u> print <i>Armstrong Number</i>
   Step 8 → If <u>Sum not equals to Arms</u> print <i>Not Armstrong Number</i>
STOP

伪代码 (Pseudocode)

我们可以按如下方式起草上述算法的伪代码 -

procedure armstrong : number
   check = number
   rem   = 0
   WHILE check IS NOT 0
      rem ← check modulo 10
      sum ← sum + (rem)<sup>3</sup>
      divide check by 10
   END WHILE
   IF sum equals to number
      PRINT armstrong
   ELSE
      PRINT not an armstrong
   END IF
end procedure

实现 (Implementation)

下面给出该算法的实现。 您可以更改arms变量的值并执行并检查您的程序 -

#include <stdio.h>
int main() {
   int arms = 153; 
   int check, rem, sum = 0;
   check = arms;
   while(check != 0) {
      rem = check % 10;
      sum = sum + (rem * rem * rem);
      check = check/10;
   }
   if(sum == arms) 
      printf("%d is an armstrong number.", arms);
   else 
      printf("%d is not an armstrong number.", arms);
   return 0;
}

输出 (Output)

该方案的产出应该是 -

153 is an armstrong number.
↑回到顶部↑
WIKI教程 @2018