Description
Write a program that reads an interger n, and then displays 2^0, 2^1, ... 2^i-1, ... 2^0 for each line, where i = n...1.
For example, if n=4, the pattern is as follow:
1 2 4 8 4 2 1
1 2 4 2 1
1 2 1
1
Each number shall be output in a specific width of 8, justified to the right.
Input Format
An integer n (1<=n<=15).
Output Format
The Pattern of height n.
Sample Input
4
Sample Output
1 2 4 8 4 2 1
1 2 4 2 1
1 2 1
1
#include<iostream> #include <iomanip> using namespace std; int main() { int n, num[16] = { 1 }, i,j; for (i = 1; i < 16; i++) num[i] = num[i-1]*2; while (cin >>n) { for (i = 0; i < n; i++) { for (j = 0; j < i; j++) cout << " "; for (j = 0; j < n - i; j++) cout << setiosflags(ios::right) << setw(8) << num[j]; for (j = n-i-2; j >=0; j--) cout << setiosflags(ios::right) << setw(8) << num[j]; cout << endl; } } return 0; }