#include <stdio.h>
int narcissistic( int number );
void PrintN( int m, int n );
int main()
{
int m, n;
scanf("%d %d", &m, &n);
if ( narcissistic(m) ) printf("%d is a narcissistic number\n", m);
PrintN(m, n);
if ( narcissistic(n) ) printf("%d is a narcissistic number\n", n);
return 0;
}
/* 你的代码将被嵌在这里 */
int narcissistic( int number )//判断number是否为水仙花数
{
int x; //x分离出的每位数字
int count = 0; //count统计数的位数
int t; //等于number的临时变量
int sum = 0;//每个数字的幂之和
int pow; //幂
t = number;
while(t!=0)
{
count++;//计算number的位数
t/=10;
}
t=number;
for(int i=0;i<count;i++)
{
x=t%10;
pow=1;
for(int j=0;j<count;j++)//求各位数的幂次
{
pow=pow*x;
}
sum+=pow;
t/=10;
}
if(sum==number)
{
return 1;
}
else{
return 0;
}
}
void PrintN( int m, int n )
{
for(int i=m+1;i<n;i++)
{
if(narcissistic(i)==1)
{
printf("%d\n",i);
}
}
}
实验5-9 使用函数输出水仙花数 (20分)
猜你喜欢
转载自blog.csdn.net/qq_38054511/article/details/104237965
今日推荐
周排行