Description
Write a program that reads an interger n, then display the n levels pyramid pattern. For example, if n=4, the pattern is as follow:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
Input
An integer n (1<=n<=8).
Output
The n levels Pattern.
You should specify the width of each number’s print field to 5, justify the output to right.
NOTE:
There is NO SPACE at the end of each line.
There is A NEWLINE (\n) at the end of last line.
That is (. indicates and ¬ indicates <newline>)
..........1¬
.....1....2....1¬
Sample Input
4
Sample Output
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
作业记录。
#include<stdio.h>
int main(){
int n,t=1,b=1;
scanf("%d",&n);
for(int i=1;i<=n;i++){
double a=1;
printf("%*c",5*(n-i+1)-1,32);
printf("%d",1);
for(t=2;i>1&&t<2*i;t++){
for(;t<=i;t++){
b=b*2;
printf("%5d",b);}
for(;t>i&&t<2*i;t++){
b/=2;
printf("%5d",b);
}
}
printf("\n");
}
return 0;
}