3508 - 编程计算(3)
打印所有的“水仙花数”。所谓“水仙花数”,是指一个三位数,其各位数字的立方和等于该数本身。例如,153是“水仙花数”,因为153=13+33+53。
Input
无输入
Output
输出所有的水仙花数。
Examples
Input
no input needed
Output
153 370 371 407
Solution C
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h> int main() { // freopen("in","r",stdin); // freopen("out","w",stdout); int i, j, k, n; for (n = 100; n < 1000; n++) { i = n / 100; j = (n - i * 100) / 10; k = n % 10; if (i*100 + j*10 + k == i*i*i + j*j*j + k*k*k) { printf("%d\n",n); } } return 0; }
Solution C++
#include<stdio.h> int main() { int n,a,b,c; for(a = 1;a <= 9;a++) { for(b = 0;b <= 9;b++) { for(c = 0;c <= 9;c++) { if(a*100 + b*10 + c == a*a*a + b*b*b + c*c*c) { n = a*100 + b*10 + c; printf("%d\n",n); } } } } return 0; }