函数接口定义:
int narcissistic( int number );
void PrintN( int m, int n );
函数narcissistic
判断number
是否为水仙花数,是则返回1,否则返回0。
函数PrintN
则打印开区间(m
, n
)内所有的水仙花数,每个数字占一行。题目保证100≤m
≤n
≤10000。
#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;
}/* 你的代码将被嵌在这里 */
153 400
153 is a narcissistic number
370
371
#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) { if(3<=number) { int a,b,sum=0; b=number; while(b>0) { a = b%10; sum = sum + a*a*a; b /=10; } if(sum == number) { return 1; } else return 0; } } void PrintN( int m, int n ) { if(100<m && n<10000)//主函数中已经判断边际m、n,这里就不要加= { int i; for(i=m+1;i<=n-1;i++) { if(narcissistic(i)) { printf("%d\n", i); } } } }