水仙花数的简便算法

一、水仙花数是什么呢?

“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身, ABC=AAA+BBB+CCC。比如:153=13+53+3^3。 现在要求输出所有在m和n范围内的水仙花数。

二、 执行代码:

#include <stdio.h>
#include <windows.h>
#include <math.h>
int main()
{
int num = 0;
int a = 0;
int b = 0;
int c = 0;

for (num = 100; num < 1000; num++);//给定num的范围,在100到1000之间
for (a = 1; a < 10; a++)
	for (b = 0; b < 10; b++)
		for (c = 0; c < 10; c++)//a,b,c为个位数字,因为num大于100所以a要大于0
		{
			if (a * 100 + b * 10 + c == a * a*a + b * b*b + c * c*c)//按照公式输入即可
				printf("num=%d%d%d\n", a, b, c);

		}
system("pause");
return 0;

三、运行结果:

水仙花数为:
153
370
371
407

猜你喜欢

转载自blog.csdn.net/weixin_43755584/article/details/84851447