例如:輸入5,輸出:
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
- import java.util.Scanner;
- public class C1
- {
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- System.out.println("請輸入層數");
- int linenumber = input.nextInt();
- for(int n = 0 ; n <= linenumber ; n ++)//控制換行
- {
- for(int i = linenumber - 1 ; i >= n ; i--)//控制印空白
- {
- System.out.print(" ");
- }
- for(int y = 0 ; y <= n ; y++ )//印出數字
- {
- int a = (int) Math.pow(2 , y) ;
- System.out.printf("%-3d" , a);
- }
- while(n >= 1)
- {
- for(int z = n-1 ; z >= 0 ; z -- )//印出數字
- {
- int b = (int) Math.pow(2 , z);
- System.out.printf("%-3d" , b);
- }
- break ;
- }
- System.out.println();
- }
- }
- }
沒有留言:
張貼留言