统计信息

  • 访问数:11186
  • 博客数:142
  • 建立时间:2008-01-05
  • 更新时间:2008-05-21
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P4Q3

2008-02-13 17:35:34

天气: 晴朗 心情: 高兴

/*P4Q3
 * Name:Wang Chaoran
 * Description:
 * 3 (Printing numbers in a pyramid pattern)
Write a nested for loop that prints the following output:

                          1
                     1    2   1
                 1   2    4   2   1
             1   2   4    8   4   2   1
         1   2   4   8   16   8   4   2  1
      1  2   4   8  16   32  16   8   4  2  1
   1  2  4   8  16  32   64  32  16   8  4  2  1
1  2  4  8  16  32  64  128  64  32  16  8  4  2  1
*/
class P4Q3
{
  public static void main(String[]args)
  {
   
    for(int i=1;i<=8;i++)
    {
      int b1=1;
      double c1=Math.pow(2,i);
      //Print the spaces
    for(int j=8;j>=i;j--)
    {
      System.out.printf("%4s","");
    }
    //Print the ascending part of the numbers
    if(i!=1) 
    {
      for(int b=1;b<=i-1;b++)
      {
       System.out.printf("%4d",b1);
        b1=b1*2;              
      }
    }
    //Print the descending part of the numbers
      for(int c=1;c<=i;c++)
      {
             
       c1=c1/2;
        System.out.printf("%4.0f",c1);
  
      }
      System.out.println();
      }
     
      }       

     
   
 


   


加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd