习题5-6 使用函数输出水仙花数 (20分)

#include 

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)
{
    int t;
    int sum = 0,ref = 0, cnt = 0;
    t=number;
    while (t)
    {
        t /= 10;
        cnt++;
    }
    t = number;
    while (t)
    {
        int j=cnt,tem = 1;
        while (j--)
        {
            tem *= t % 10;
        }
        sum += tem;
        t /= 10;
    }
    if (sum == number)
    {
        ref = 1;
    }
    return ref;
}
void PrintN(int m, int n)
{
    for (int i = m+1; i < n; i++)
    {
        if (narcissistic(i))
        {
            printf("%d\n", i);
        }
    }
}

你可能感兴趣的:(习题5-6 使用函数输出水仙花数 (20分))