1. 程式人生 > 其它 >輸出所有的水仙花

輸出所有的水仙花

技術標籤:java

解法一

public class shuixianhua {
    public static void main(String[] args) {
        for (int i = 100; i <= 999; i++) {
            int m=i/100;
            int n=(i/10)%10;
            int k=i-m*100-n*10;
            if (Math.pow(m,3)+Math.pow(n,3)+Math.pow(k,3)== i){
                System.out.
println(i); } } } }

解法二

public class shuixianhau {
    public static void main(String[] args) {
        for (int x = 1; x <= 9 ; x++) {
            for (int y = 0; y <= 9 ; y++) {
                for (int z = 0; z <=9 ; z++) {
                    int i=x*100+y*10+z;
                    if
(x*x*x+y*y*y+z*z*z== i){ System.out.println(i); } } } } } }