黑马程序员技术交流社区

标题: 请用for循环实现。 [打印本页]

作者: 王春涛    时间: 2014-5-20 08:52
标题: 请用for循环实现。
本帖最后由 王春涛 于 2014-5-20 09:56 编辑

我国最高山峰是珠穆朗玛峰,8848米。现在我有一张足够大的纸,它的厚度是0.01米。
请问,我折叠多少次,可以折成珠穆朗玛峰的高度。

作者: skill20    时间: 2014-5-20 09:41
2的n次方?
作者: 沉默的爱    时间: 2014-5-20 09:46
class forDemo
{
        public static void main(String[] args)
        {
                final double HIGH=8848;
                double start=0.01;
                for (int i=1;start<HIGH ;i++ )
                {
                        start=start*2;
                        System.out.println(i+"        "+start);
                }
        }
}
i表示折的次数,start表示折过后的厚度!!
不知道对不对!!
作者: 沉默的爱    时间: 2014-5-20 09:47
1       0.02
2       0.04
3       0.08
4       0.16
5       0.32
6       0.64
7       1.28
8       2.56
9       5.12
10      10.24
11      20.48
12      40.96
13      81.92
14      163.84
15      327.68
16      655.36
17      1310.72
18      2621.44
19      5242.88
20      10485.76


运行结果是这样的!!
作者: 王春涛    时间: 2014-5-20 09:56
沉默的爱 发表于 2014-5-20 09:46
class forDemo
{
        public static void main(String[] args)

谢谢 :handshake
作者: 小小6456    时间: 2014-5-20 10:08
学习了。。。。。。。。。。
作者: 沉默的爱    时间: 2014-5-20 10:08
王春涛 发表于 2014-5-20 09:56
谢谢

小小事了!!
作者: skill20    时间: 2014-5-20 10:21
  1. public static void pow(){
  2.                 /*int count = 0;
  3.                 while(0.01*Math.pow(2, count) <= 8848)
  4.                         count++;
  5.                 System.out.println(count);*/
  6.                 int count = 0;
  7.                 for(int x = 0; 0.01 * Math.pow(2, x) <= 8848; x++)
  8.                         count++;
  9.                 System.out.println(count);
  10.         }
复制代码

作者: gentleman    时间: 2014-5-20 10:37
  1. public class Shan
  2. {
  3.         public static void main (String[] args)
  4.         {
  5.                 int n=0;
  6.                
  7.                 final double HIGH = 8848;
  8.                 for( double start=0.02;start<HIGH;n++)       
  9.                 {
  10.                         start=Math.pow(2, n)*0.02;
  11.                         System.out.println(start);
  12.                 }
  13.                 System.out.println(n);
  14.         }
  15. }
  16. 也可以用Math类做,n表示折叠的次数,start表示折纸的高度
复制代码

作者: 海世山盟    时间: 2014-5-20 10:53
        public static void times()
        {
                double h=8848;
                double s=0;
                double start=0.01;
                int count=0;
                for(s=0;s<=h;) //[color=Red这里我将s的变化情况放在了for的执行代码里面,但是该处的分号是不能少的。
                {
                s=s+start;
                start=start*2;//没跌一次,其高度是成倍增长的
                count++;
                }
System.out.println(s);
System.out.println(count);
                }
作者: 复仇者联盟    时间: 2014-6-29 22:44
其实用while循环也挺不错的,也比较简单。
class ZhuMu
{
        public static void main(String[] args)
        {
                double a=0.01;
                int num=0;
                while(a<8848)
                {
                        a*=2;
                        num++;
                        System.out.println(a);
                }
                System.out.println(num);
        }
}

作者: 复仇者联盟    时间: 2014-7-1 00:27
原题中纸的厚度应该是0.01厘米吧。
class LianXi8
{
        public static void main(String[] args)
        {        int num=0;
                for(int i=1;i<88480000;num++)
                {
                        i*=2;
                        System.out.println(i);
                }
                System.out.println(num);
        }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2