for循環練習題1——水仙花數


/*
輸出所有的水仙花數,
所謂水仙花數是指一個3位數,
其各個位上數 字立方和等於其本身。
例如: 153 = 1*1*1 + 3*3*3 + 5*5*5

*/
class ForTest3{

  public static void main(String[] args){
    int num1 = 0;//百位
    int num2 = 0;//十位
    int num3 = 0;//個位
    for(int i = 100; i < 1000; i++){
      num1 = i/100;//獲取三位數中的百位數字
      num2 = i%100/10;//獲取三位數中的十位數字
      num3 = i%100%10;//獲取三位數中的個位數字
      //獲取三次方
      double c1 = Math.pow(num1,3);
      double c2 = Math.pow(num2,3);
      double c3 = Math.pow(num3,3);
      //判斷
      if((c1+c2+c3) == i){
      System.out.println(i);
      }
    }
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM