两个整数之间的阿姆斯特朗数?

如果整数被分离出来并求出立方并求和,则该整数称为n阶阿姆斯特朗数,则总和与该数字相同,即abcd ... = a 3 + b 3 + c 3 + d 3 + .. 。

如果是3位的阿姆斯特朗数,则每个数位的立方和等于该数字本身。例如:

153 = 1 3 + 5 3 + 3 3 // 153是一个阿姆斯特朗数。

Input: Enter two numbers(intervals):999 9999
Output: Armstrong numbers between 999 and 9999 are: 1634 8208 9474

说明

1634 = 13+63+33+43= 1+216+27+64
= 1634

下面实现的方法很简单。我们遍历给定范围内的所有数字。对于每个数字,我们首先计算其中的数字。令当前数字中的位数为n。他们找到了所有数字的立方和。如果总和等于I,我们打印数字。

示例

#include <stdio.h>
#include <math.h>
int main() {
   int low = 100;
   int high = 400;
   printf("The amstrong numbers between %d and %d is \n",low,high);
   for (int i = low+1; i < high; ++i) {
      int x = i;
      int n = 0;
      while (x != 0) {
         x /= 10;
         ++n;
      }
      int pow_sum = 0;
      x = i;
      while (x != 0) {
         int digit = x % 10;
         pow_sum += pow(digit, n);
         x /= 10;
      }
      if (pow_sum == i)
         printf("%d ", i);
   }
   printf("\n");
   return 0;
}