求出100~999之间的所有“水仙花数”并输出。

求出100~999之间的所有“水仙花数”并输出。

在数论中,水仙花数(Narcissistic number)也称为自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(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。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <math.h>
int main() {
	int i = 0;
	for (i = 100; i <= 999; i++) {//遍历100-999的每一个数
		int a = i % 10;//取出三位数的个位
		int b = i / 10 % 10;//取出三位数的十位
		int c = i / 100;//取出三位数的百位
		//pow(x,y)求x的y次方,添加头文件<math.h>
		if ((pow(a , 3) + pow(b , 3) + pow(c , 3)) == i) {
			printf("%d  ", i);
		}
	}
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/a_hang_szz/article/details/88773949