#include<stdio.h> #include<stdlib.h> int main() { int a, b, c,sum; for (a = 1; a <= 9; a++) { for (b = 0; b <= 9; b++) { for (c = 0; c <= 9; c++) { sum = a * 100 + b * 10 + c; if (sum == a*a*a + b*b*b + c*c*c) printf("%d\n", sum); } } } system("pause"); return 0; }
方法二
#include<stdio.h> int main() { int i, j, k; int sum; printf("输出的水仙花数是:"); for (sum = 100; sum <= 999; sum++) { i = sum / 100; j = (sum - i * 100) / 10; k = sum % 10; if (sum == (i*i*i + j*j*j + k*k*k)) printf("%d\n", sum); } printf("\n"); system("pause"); return 0; }
方法三:
#include<stdio.h> #include<stdlib.h> #include<math.h> //pow int main() { int i; printf("水仙花数:\n"); for (i = 100; i < 1000; i++) { int sum = 0; int num = i; while (num) { sum = sum + pow((num % 10), 3);//pow立方函数 num /= 10; } if (i==sum) { printf("%d ", sum); } } system("pause"); return 0; }