Printing numbers in a pyramid pattern(一)

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值