Welcome to my blog.|

Khru

园龄:2年11个月粉丝:22关注:1

📂C
2023-03-24 19:08阅读: 16评论: 0推荐: 0

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;
}

本文作者:Khru

本文链接:https://www.cnblogs.com/khrushchefox/p/17253086.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Khru  阅读(16)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起