/*
我国最高山峰是珠穆朗玛峰,8848米。现在我有一张足够大的纸,它的厚度是0.01米。
请问,我折叠多少次,可以折成珠穆朗玛峰的高度
思路:
1、因为结果要的是折叠次数,所以可以将double类型的0.01转换成int类型的1,山峰高度就变成884800
2、每次折叠都是相当于乘以2,定义变量int start = 1,int end = 884800.定义变量折叠次数为count
3、使用for循环遍历
*/
class Demo4{
//方法一:使用for循环
public static void main(String[] args){
int end1 = 884800;
int count1 = 0;
for (int start = 1;start <=end1 ;start*=2 ){
count1++;
}
System.out.println("for方法:"+count1);
//方法二:使用while循环
int start = 1;
int end2 = 884800;
int count2 = 0;
while (start<=end2){
count2++;
start*=2;
}
System.out.println("while方法:"+count2);
}
}
|
|