打印100-999之间的水仙花数
水仙花数是指一个3位数,它的各位数字立方和等于该数本身
例如153=(1*1*1)+(5*5*5)+(3*3*3)

解决方案 »

  1.   

    随便写了下public class Test {
                public Test() {
         this.t();
        }
        
        public void t(){
         for (int i=100;i<=999;i++){
    if (lifang(i/100)+lifang(i%100/10)+lifang(i%10%10)==i) System.out.println(i+"  ");
         }
         }
        public int lifang(int n){
         return n*n*n;
         }    public static void main(String[] args) {
            new Test();
        }
    }
      

  2.   

    这个题目考察的应该只是用最基本的东西,不用类方法做:
    public class Narcissistic
    {
      public static void main(String[] args)
      {
        int number, hundredsPlace, tensPlace, onesPlace;
        for (number = 100; number < 999; number++)
        {
          hundredsPlace = number / 100;
          tensPlace = number % 100 / 10;
          onesPlace = number % 10;
          if ( Math.pow(hundredsPlace, 3) + Math.pow(tensPlace, 3) + Math.pow(onesPlace, 3) == number)
            System.out.println(number);
        }
      }
    }
      

  3.   


    谢谢这位大大,刚才俺把Math.pow()忘了,又加深了印象呵呵