java水仙花數程式碼,三位四位五位

魚鰾。發表於2020-12-10

ShuiXianHua.java

public class ShuiXianHua{
    public static void main(String[] args){
        for(int i=100;i<1000;i++){
            int x =i/100;
            int y =i/10%10;
            int z =i%10;
            if (x*x*x + y*y*y + z*z*z == i){
                System.out.println(x+","+y+","+z+"|"+"三位水仙花數是:"+i);
            }
        }System.out.println("======再來看看其他的唄======");
        for(int j=1000;j<10000;j++){
            int a =j/1000;
            int b =j/100%10;
            int c =j/10%10;
            int d =j%10;
            if(a*a*a*a + b*b*b*b + c*c*c*c +d*d*d*d == j) {
                System.out.println(a+","+b+","+c+","+d+"|"+"四位水仙花是:"+j);
            }
        }System.out.println("======還有還有,別走,再看看唄======");
        for(int o=10000;o<100000;o++){
            int q=o/10000;
            int w=o/1000%10;
            int e=o/100%10;
            int r=o/10%10;
            int t=o%10;
            if(q*q*q*q*q + w*w*w*w*w + e*e*e*e*e + r*r*r*r*r + t*t*t*t*t ==o){
                System.out.println(q+","+w+","+e+","+r+","+t+"|"+"五位水仙花是:"+o);
            }
        }
    }
}
輸出結果:
1,5,3|三位水仙花數是:153
3,7,0|三位水仙花數是:370
3,7,1|三位水仙花數是:371
4,0,7|三位水仙花數是:407
======再來看看其他的唄======
1,6,3,4|四位水仙花是:1634
8,2,0,8|四位水仙花是:8208
9,4,7,4|四位水仙花是:9474
======還有還有,別走,再看看唄======
5,4,7,4,8|五位水仙花是:54748
9,2,7,2,7|五位水仙花是:92727
9,3,0,8,4|五位水仙花是:93084

相關文章