基础练习题

3 水仙花数

2023-09-12  本文已影响0人  北极的大企鹅

  @SuppressWarnings({"rawtypes","unchecked"})
  public class _003ShuiXianFlower {
     public static int b, s, g;
  
      public boolean isDaffodilNumber(int i) {
          boolean flag = false;
          // 个位数
          g = i % 10;
          // 十位数
         s = (i / 10) % 10;
         // 百位数
         b = i / 100;
 
         if (i == ((g * g * g) + (s * s * s) + (b * b * b))) {
             flag = true;
         }
         return flag;
     }
 
     public static void countFlower() {
         int x = 0;
         Vector v = new Vector();
         _003ShuiXianFlower db = new _003ShuiXianFlower();
         for (int i = 100; i < 1000; i++) {
             if (db.isDaffodilNumber(i) == true) {
                 v.add(i);
                 x++;
             }
         }
         System.out.println("100到1000之间有" + x + "个水仙花数");
         System.out.println("所有的水仙花数为:" + v);
     }
 
     public static void main(String[] args) {
         countFlower();
     }
 }

上一篇下一篇

猜你喜欢

热点阅读