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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步