我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*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();
}
}
}
导入论坛查看(62)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing