C-水仙花数

发布时间 2023-03-24 19:15:20作者: Khrushchefox

水仙花数

水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number)。

水仙花数是指一个3 位数,它的每个位上的数字的3次幂之和等于它本身。

例如:1^3 + 5^3+ 3^3 = 153。

#include <stdio.h>
#include <math.h>

int main()
{
    int unit, ten, hundred, result;

    for(int i = 100; i < 1000; i++)
    {
        unit = i%10;
        ten = (i/10)%10;
        hundred = i/100;
        result = (int) (pow(unit, 3)+pow(ten, 3)+pow(hundred, 3));
        if (result == i)
        {
            printf("%d\n", i);
        }
    }
    return 0;
}