用java求1000之内所有的 “水仙花数 “,所谓 “水仙花数 “是指一个三位数,其各位数字立方和等于该数本身

/**

 * 计算三位数的每位数的立方和相加后等于这个数

 */

public class Test {

 

    public staticvoid main(String[] args) {

       //1.方法一

        for (int k =100; k <= 999; k++) {

           int a = k / 100;

           int b = k / 10 % 10;

           int c = k % 10;

           if (k == (a*a*a+b*b*b+c*c*c)) {

              System.out.println(k);

           }

       }

       System.out.println();

 

       //方法二

       for (int i = 1; i <= 9; i++) {

           for (int j = 0; j <= 9; j++) {

              for (int k = 0; k <= 9; k++) {

                  int a = i * 100;

                  int b = j * 10;

                  int c = k;

                  int num = a + b + c;

                  if (num == (i * i * i + j * j * j + k * k * k)) {

                     System.out.println(num);

                  }

              }

           }

       }

    }

}

你可能感兴趣的:(java)