java复习与整理195
-
package demo02; public class Application { public static void main(String[] args) { /* 需求:世界最高峰珠穆朗玛峰为8848.43m = 8848430mm ,一张纸的厚度为0.1毫米,若纸无限大, 则需要对折多少次能到达珠穆朗玛峰的高度? */ Test s1 = new Test(); s1.high(); } } -------------------------------------------- package demo02; public class Test { public void high(){ int H = 88484300; // 便于好算,扩大10倍 int i = 1; int n = 0; while ( i < H){ i = i*2; n++; } System.out.println("需要对折"+n+"次"); } } ========================================== 需要对折27次 Process finished with exit code 0
-
package demo01; public class Application { // 遍历100到999的水仙花数 public static void main(String[] args) { Test n1 = new Test(); n1.calculate(); } } -------------------------------------- package demo01; public class Test { public void calculate(){ int n = 0; int m =0; for (int i = 152; i < 999 ; i++) { int a = i/100; // 百位上的数字 int b = i/10%10; // 十位上的数字 int c = i%10; // 各位上的数字 if ( i == a*a*a + b*b*b + c*c*c ){ n = i; m++; System.out.print(n+" "); } } System.out.println(); System.out.println("水仙花数的个数为:"+m); } } ===================================== 水仙花数的个数为:4 Process finished with exit code 0