C++ 打印空心金字塔、菱形图案及其修改的程序(Program to print hollow pyramid, diamond pattern and their modifications)

1. 空心金字塔/三角形模式 
该模式类似于金字塔模式。唯一的区别是,我们将用空格字符替换所有内部“#”或“*”字符,并在最后一行打印 2*N-1(N = 模式中的行数)“#”或“*”字符。 

示例: 

输入:n=6

输出:

     #
    # #
   #   #
  #     #
 #       #
#         #
#######

实际输出: 

输出:

     #
    # #
   #   #
  #     #
 #       #
#         #
#######

实际输出: 

时间复杂度:O(N^2),此算法的时间复杂度为 O(N^2),其中 N 是行数。这是因为我们要循环遍历模式的行和列,这需要 O(N^2) 的时间才能完成。

空间复杂度:O(1),该算法不需要任何额外的空间,因此其空间复杂度为O(1)。

2. 空心钻石

注意:对于偶数输入,打印 n-1 的模式。

例子:

输入: 1

输出:


对于 n=1

输入: 7

输出:

对于 n=7 

输入: 9

输出:

对于 n=9 

方法:要打印钻石,我们需要在星星之前和星星之后打印空格,以实现星星距离的不断增加。

        要打印盒子形状,我们需要为 i==1(第一行)和 i==n(最后一行)打印 '-',为 j==1(第一列)和 j==n(最后一列)打印 '|'。

算法: 1.如果 n 为奇数,则增加 n。

2. 求 mid=n/2。

3. 从 1 遍历到中间,打印图案的上半部分(比如 i)。

4. 从 1 遍历到 mid-i 来打印最左上角外框(比如 j)的空格。

5. 如果 (i==1),则打印“*”(因为第一行我们只需要一颗星)。

6. 否则打印‘*’并从 1 遍历到 2*i-3 以打印空心菱形的空间(比如 j),并在循环结束后打印‘*’。

7. 从 1 遍历到 mid-i,再次打印右上角最外层框(例如 j)的空格。

8. 在步骤 3 处闭合循环。

9. 从 mid+1 遍历到 n-1 来打印模式的下半部分(比如 i)。

4. 从 1 遍历到 i-mid 来打印最左下角外框(比如 j)的空格。

5. 如果 (i==n-1) 则打印‘*’(因为最后一行我们只需要一颗星)。

6. 否则打印‘*’并从 1 遍历到 2*(ni)-3 以打印空心菱形的空间(比如 j),并在循环结束后打印‘*’。

7. 从 1 遍历到 i-mid,再次打印最右下角外框(例如 j)的空格。

8. 在步骤 9 处关闭循环。

#include <bits/stdc++.h>
using namespace std;
 
// function to print the pattern
void printPattern(int& n)
{
    int i,j,mid;
    if(n%2==1) //when n is odd, increase it by 1 to make it even
      n++;
    mid = n/2;
     
    // upper half pattern
    for(i = 1; i<= mid; i++) {
      for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box before star
         cout<<" ";
          
      if(i == 1) {
         cout << "*";
      }else{
         cout << "*"; //in each line star at start and end position
         for(j = 1; j<=2*i-3; j++) { //print space to make hollow
            cout << " ";
         }
         cout << "*";
      }
      for(j = 1; j<=mid-i; j++) //print the blank spaces and outer box after star
         cout<<" ";
          
      cout << endl;
   }
    
   // lower half pattern
   for(i = mid+1; i<n; i++) {
        
      for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box before star
         cout<<" ";
          
      if(i == n-1) {
         cout << "*";
      }else{
         cout << "*"; //in each line star at start and end position
         for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow
            cout << " ";
         }
         cout << "*";
      }
      for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star
         cout<<" ";
          
      cout << endl;
   }
}
 
// driver's code
int main() {
   int n=7;
   printPattern(n);
}
// this code is contributed by prophet1999

输出:
   *   
  * *  
 *   * 
*     *
 *   * 
  * *  
   *   
   

时间复杂度:给定输入 n,O(n^2)

辅助空间:O(1)

3. 空心菱形位于由水平和垂直虚线 (-) 组成的矩形框内

        编写一个程序,打印由破折号(-)和按位或(|)组成的框内的空心菱形图案,如下所示。

注意:对于偶数输入,打印n-1的模式。

例子:

输入: 1

输出: 

对于 n=1 

输入: 7

输出:

对于 n=7

输入: 9

输出:

对于 n=9 

方法:要打印钻石,我们需要在星星之前和星星之后打印空格,以实现星星距离的不断增加。

        要打印盒子形状,我们需要为 i==1(第一行)和 i==n(最后一行)打印 '-',为 j==1(第一列)和 j==n(最后一列)打印 '|'。

算法: 1.如果 n 为奇数,则增加 n。

2. 求 mid=n/2。

3. 从 1 遍历到中间,打印图案的上半部分(比如 i)。

4. 从 1 遍历到中间 i 来打印最左上角的外框(比如 j)。

5. 如果 (i==1),则打印“*”(因为第一行我们只需要一颗星)。

6. 否则打印‘*’并从 1 遍历到 2*i-3 以打印空心菱形的空间(比如 j),并在循环结束后打印‘*’。

7. 从 1 遍历到中间 i 来打印最右上角的外框(比如 j)。

8. 在步骤 3 处闭合循环。

9. 从 mid+1 遍历到 n-1 来打印模式的下半部分(比如 i)。

4. 从 1 遍历到 i-mid 来打印最左下角的外框(比如 j)。

5. 如果 (i==n-1) 则打印‘*’(因为最后一行我们只需要一颗星)。

6. 否则打印‘*’并从 1 遍历到 2*(ni)-3 以打印空心菱形的空间(比如 j),并在循环结束后打印‘*’。

7. 从 1 遍历到 i-mid 来打印最右下角的外框(比如 j)。

8. 在步骤 9 处关闭循环。

#include <bits/stdc++.h>
using namespace std;
 
// function to print the pattern
void printPattern(int& n)
{
    int i,j,mid;
    if(n%2==1) //when n is odd, increase it by 1 to make it even
      n++;
    mid = n/2;
     
    // upper half pattern
    for(i = 1; i<= mid; i++) {
      for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box before star
         if(i==1)
         cout<<"-";
         else if(j==1)
         cout << "|";
         else cout<<" ";
      }
      if(i == 1) {
         cout << "*";
      }else{
         cout << "*"; //in each line star at start and end position
         for(j = 1; j<=2*i-3; j++) { //print space to make hollow
            cout << " ";
         }
         cout << "*";
      }
      for(j = 1; j<=mid-i; j++) { //print the blank spaces and outer box after star
         if(i==1)
         cout<<"-";
         else if(j==mid-i)
         cout << "|";
         else cout<<" ";
      }
      cout << endl;
   }
    
   // lower half pattern
   for(i = mid+1; i<n; i++) {
        
      for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box before star
         if(i==n-1)
         cout<<"-";
         else if(j==1)
         cout << "|";
         else cout<<" ";
      }
      if(i == n-1) {
         cout << "*";
      }else{
         cout << "*"; //in each line star at start and end position
         for(j = 1; j<=2*(n - i)-3; j++) { //print space to make hollow
            cout << " ";
         }
         cout << "*";
      }
      for(j = 1; j<=i-mid; j++) { //print the blank spaces and outer box after star
         if(i==n-1)
         cout<<"-";
         else if(j==i-mid)
         cout << "|";
         else cout<<" ";
      }
      cout << endl;
   }
}
 
// driver's code
int main() {
   int n=12;
   printPattern(n);
}
// this code is contributed by prophet1999

输出:

-----*-----
|   * *   |
|  *   *  |
| *     * |
|*       *|
*         *
|*       *|
| *     * |
|  *   *  |
|   * *   |
-----*-----

时间复杂度: O(n*n)

辅助空间: O(1)

  • 30
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值