水仙

算法之水仙花数(Java语言)

☆樱花仙子☆ 提交于 2020-01-25 19:45:25
概述 在 数论 中, 水仙花 数 ( Narcissistic number ),也被称为 超完全数字不变数 ( pluperfect digital invariant, PPDI )、 自恋 数 、 自幂数 、 阿姆斯壮 数 或 阿姆斯特朗数 ( Armstrong number ) ,用来描述一个 N 位非负整数,其各位数字的 N 次方和等于该数本身。 举例 例如153、370、371及407就是三位超完全数字不变数,其各个数之立方和等于该数: 153 = 1 3 + 5 3 + 3 3 。 370 = 3 3 + 7 3 + 0 3 。 371 = 3 3 + 7 3 + 1 3 。 407 = 4 3 + 0 3 + 7 3 。 Java算法 1 /** 2 * A Narcissistic number is a number that is the sum of its own digits each 3 * raised to the power of the number of digits. E.g., 0, 1, 2, 3, 4, 5, 6, 7, 8, 4 * 9, 153, 370, 371, 407, 1634, 8208, 9474. 5 */ 6 public class NarcissisticNumberExample { 7 /

编写一个程序,输出所有水仙花数,并统计共有多少个水仙花数。所谓水仙花数是指一个3位数,其各位数字立方和等于该数本身。例如,153=1^3+5^3+3^3

做~自己de王妃 提交于 2019-11-28 18:45:07
#include <stdio.h> int main() { int i, j, k, count = 0; //声明计数器变量 printf("找到的水仙花数有:\n"); //输出提示信息 for (i = 1; i <= 9; i++) //百位从1~9中枚举 for (j = 0; j <= 9; j++) //十位从0~9中枚举 for (k = 0; k <= 9; k++) //个位从0~9中枚举 { if (i * 100 + j * 10 + k == i * i * i + j * j * j + k * k * k) //判断是否为水仙花 { printf("%d ", i * 100 + j * 10 + k); //输出一个水仙花数 count++; //统计水仙花数个数 if (count % 5 == 0) //每五个数一行 printf("\n"); } } printf("\n共有%d个水仙花数\n", count); return 0; } 来源: https://blog.csdn.net/zhushidaji2020/article/details/100108682