求出0~100000之间的所有“水仙花数”并输出。

在数论中,水仙花数(Narcissistic number)也称为自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),是指一n位数,其各位数字的n次方和等于该数。
比如:
153 = 1^3 + 5^3 +3^3
此程序要运用到math函数库的pow()函数

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
    
    
 int n;
 int a[10] = {
    
     0 };
 int i;
 int sum = 0;
 int count = 0;
 for (n = 0; n <= 100000; ++n){
    
     //遍历0-100000之间所有数字
  for (i = n; i; i /= 10){
    
    
   a[count] = i % 10;  //用数组a[count]将每一位存起来,用count记下数字的位数
   ++count; 
  }
  for (i = 0; i < count; ++i){
    
    
   sum += pow(a[i],count); //将数组中所存的数字的每一位进行对应的位数count次方并求和
  }
  if (n == sum){
    
    
   printf("%d \n",n); 
  }
  count = sum = 0; //强制将count与sum归0
 }
 return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/WEIYANGBIN/article/details/106045895