首页 > 代码库 > 算法15---数论3---水仙花数

算法15---数论3---水仙花数

算法15---数论3---水仙花数

简单地说。三维正整数在树枝上等于其各位数字的立方之和,称为水仙花数;
同样我们也可以定义一些更高等级的水仙花数,比如4位的,例如1634,1634位1,6,3,4的四次方和。
 
 1 // 2 /* 3     题目:水仙花数 4     author taoliu——alex  2016.10 5  6     主要实现: 7     1 找出给定位数内的水仙花数; 8     2 判断一个数是不是水仙花数; 9 10 */11 12 13 14 #include <stdio.h>15 #include <math.h>16 17 18 //输出n为的水仙花数19 20 void shuixianhua(int n)21 {22     long start,end;23     long sum;24 25     start=(long)pow(10,n-1);26     end=(long)pow(10,n)-1;27     for (long i = start; i <=end; i++)28     {29         long num=i;30         sum=0;31         for (int j = 0; j < n; j++)32         {33             long temp=num%10;34             sum=sum+(long)pow(temp,n);35             num=(num-temp)/10;36         }37         if (sum==i)38         {39             printf("%ld\n",i);40         }41 42     }43 }44 45 46 void  shuixianhua_judge(long n)47 {48     int digit=1;49     long sum=0;50     long temp ;51     long num=n;52     long num2=n;53     while (num/10!=0)54     {55         digit=digit+1;56         num=num/10;57     }58     printf("the digit is %d\n", digit);59 60 61     for (int i = 0; i < digit; i++)62     {63         temp=num2%10;64         //printf("%ld\n",temp);65         sum=sum+(long)pow(temp,digit);66         printf("%ld\n",sum );67         num2=(num2-temp)/10;68     }69     //printf("%ld\n",sum);70     if (sum==n)71     {72         printf("%ld is shuixianhua number\n",n);73     }74     else75         printf("%ld is NOT shuixianhua number\n",n);76 77 }78 79 80 int main()81 {82      long num;83      printf("please input the number you want to judge!\n");84      scanf("%ld",&num);85      shuixianhua_judge(num);86 87 88      int n;89      printf("give the digit you want to find!\n");90      scanf("%d",&n);91      shuixianhua(n);92 93      return 0;94 }

 

算法15---数论3---水仙花数