实现方法一:
int m, sum; int i, j, k; System.out.PRintln("所有的水仙花数为:"); for (m = 100; m < 1000; m++) { // 百位数 i = m / 100; // 十位数 j = (m - i * 100) / 10; // 个位数 k = m - i * 100 - j * 10; sum = i * i * i + j * j * j + k * k * k; if (sum == m) { System.out.print(sum + " "); }实现方法二:
int m, sum; int i, j, k; System.out.println("/n所有的水仙花数为:"); for (m = 100; m < 1000; m++) { i = m % 10; j = m / 10 % 10; k = m / 100; sum = i * i * i + j * j * j + k * k * k; if (sum == m) { System.out.print(sum + " "); } }实现方法三:
System.out.println("/n所有的水仙花数为:"); for (int i = 1; i <= 9; i++) { for (int j = 0; j <= 9; j++) { for (int k = 0; k <= 9; k++) { int ijk = i * 100 + j * 10 + k; if (ijk == i * i * i + j * j * j + k * k * k){ System.out.print(ijk+" "); } } } }运行结果如下:
新闻热点
疑难解答