问题描述:
求出0~100000之间的所有“水仙花数”并输出。
“水仙花数”是指一个n位数,其各位数字的n次方之和确好等于该数本身,如:153=1^3+5^3+3^3,则153是一个“水仙花数”。
#include <stdio.h>int main()
{int n = 0; printf("输入这个数: ");scanf("%d", &n);printf("水仙花数有: \n");for (n = 0; n < 999999; n++){int a = n / 100000; //分解十万位int b = (n % 100000) / 10000; //分解万位int c = (n % 10000) / 1000; //分解千位int d = (n % 1000) / 100; //分解百位int e = (n % 100) / 10; //分解十位int f = n % 10; //分解个位//输入数是一位数if (n >= 0 && n <= 9){printf("%d ", n);}//输入数是两位数else if (n >= 10 && n <= 99){if (e * e + f * f == n)printf("%d ", n);}//输入数是三位数else if (n >= 100 && n <= 999){if (d * d * d + e * e * e + f * f * f == n)printf("%d ", n);} //输入数是四位数else if (n >= 1000 && n <= 9999){if (c * c * c * c + d * d * d * d + e * e * e * e + f * f * f * f == n)printf("%d ", n);}//输入数是五位数else if (n >= 10000 && n <= 99999){if (b * b * b * b * b + c * c * c * c * c + d * d * d * d * d + e * e * e * e * e + f * f * f * f * f == n)printf("%d ", n);}//输入数是六位数else{if (a * a * a * a * a * a + b * b * b * b * b * b + c * c * c * c * c * c + d * d * d * d * d * d + e * e * e * e * e * e + f * f * f * f * f * f == n)printf("%d ", n);}}return 0;
}
程序结果: