Description
Write a program that reads an interger n, then display one of the following patterns.
Pattern 1
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Pattern 2
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Pattern 3
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Pattern 4
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Input
An integer n (1<=n<=4).
Output
Pattern n.
You should specify the width of each number’s print field to 3, justify the output to right.
Sample Input
Copy sample input to clipboard
1
Sample Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
代码实现:
#include<iostream>
using namespace std;
void fun1(int rows)
{
for(int i = 1; i <= rows; i++){
for(int j = 1; j <= i; j++){
cout << " " << j; //两个空格
}
cout << endl;
}
}
void fun2(int rows)
{
for(int i = rows; i >= 1; i--){
for(int j = 1; j <= i; j++){
cout << " " << j; //两个空格
}
cout << endl;
}
}
void fun3(int rows)
{
for(int i = rows; i >= 1; i--){
for(int j = 0; j < i-1; j++){
cout << " "; //三个空格
}
for(int j = 1; j <= (rows+1)-i; j++){
cout << " " << j; //两个空格
}
cout << endl;
}
}
void fun4(int rows)
{
for(int i = 1; i <= rows; i++){
for(int j = 0; j < i-1; j++){
cout << " ";
}
for(int j = 1; j <= (rows+1)-i; j++){
cout << " " << j;
}
cout << endl;
}
}
int main()
{
int n;
while(cin >> n){
switch(n){
case 1: fun1(6); break;
case 2: fun2(6); break;
case 3: fun3(6); break;
case 4: fun4(6); break;
}
}
return 0;
}