输出10000以内水仙花数:
# include <stdio.h>
int main(void)
{
int i;
int m, k = 1, sum =0, j, count =0,n,a;
for(i=1;i<10000;i++)
{
n=a=i;
while(a!=0)
{
a /= 10;
count++;
}
while(n!=0)
{
j=n%10;
n /= 10;
for(m=0;m<count;m++)
k *=j;
sum += k;
k=1;
}
if(sum== i && count>=3)
printf("%d\n",i);
sum =0;
count =0;
}
return 0;
}
判断一个数是否为水仙花数:
# include <stdio.h>标签:24,count,10,int,sum,printf,基本功,水仙花 From: https://blog.51cto.com/u_15983838/6092936
int main(void)
{
int i;
printf("请输入数值:i=");
scanf("%d", &i);
int m, k = 1, sum =0, j, count =0,n,a;
n=a=i;
while(i!=0)
{
i /= 10;
count++;
}
while(n!=0)
{
j=n%10;
n /= 10;
for(m=0;m<count;m++)
k *=j;
sum += k;
k=1;
}
if(sum == a && count>=3)
printf("i是水仙花数\n");
else
printf("i不是水仙花数\n");
return 0;
}