C-水仙花数

水仙花数

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

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

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

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

int* findNarcissisticNumbers()
{
    static int narcissisticNumbers[15];
    int count = 0;
    int unit, ten, hundred, result;
    for(int i = 100; i <= 999; 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)
        {
            narcissisticNumbers[count++] = i;
        }
    }
    narcissisticNumbers[count] = -1; // add sentinel value to indicate end of array
    return narcissisticNumbers;
}

int main()
{
    int* arr = findNarcissisticNumbers();
    for(int i = 0; arr[i] != -1; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}
posted @ 2023-03-24 19:08  Khrushchefox  阅读(12)  评论(0编辑  收藏  举报