编程实现判断一个整数是否为“水仙花数”。所谓“水仙花数”是指一个三位的整数,

如题所述

不知道你要什么语言的,这里提供C语言的版本如下:

#include <stdio.h>
// 判断整数n是否为水仙花数
int isNarcissus(int n)
{
int a, b, c;

if (n >= 100 && n <= 999)
{
a = n % 10;
b = n / 10 % 10;
c = n / 100;

return (a * a * a + b * b * b + c * c * c == n);
}

return0;
}
int main()
{
int a = 153; // 设置待判断的数字
char *info = isNarcissus(a) ? "" : "NOT"; // 调用函数判断a是否为水仙花数
printf("%d is %s a narcissus number.\n", a, info); // 输出结果

return0;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2018-04-10
For[r = 100, r <= 999, r++,
If[r != (IntegerPart[
r/100])^3 + (IntegerPart[(r - 100*(IntegerPart[r/100]))/
10])^3 + (IntegerPart[
r - IntegerPart[r/100]*100 -
IntegerPart[(r - 100*(IntegerPart[r/100]))/10]*10])^3,
Continue[]]; Print[r]]
153
370
371
407
我这是Wolfram Mathematica程序,其他的你可以网上看看本回答被网友采纳
第2个回答  2021-03-16
#include <stdio.h>

int main()
{
int a,b,c,d,e;
printf("请输入一个3位整数\n");
scanf("%d",&a);
b=a/100;
c=a%100/10;
d=a%10;
e = b*b*b + c*c*c + d*d*d;
if(e == a)
printf("这个数是水仙花数",a);
else
printf("这个数不是水仙花数",a);
return 0;
}
相似回答