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 处关闭循环。
// C# program
using System;
public class GFG{
// function to print the pattern
public static 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
Console.Write(" ");
if(i == 1) {
Console.Write("*");
}else{
Console.Write("*"); //in each line star at start and end position
for(j = 1; j <= 2 * i - 3; j++) { //print space to make hollow
Console.Write(" ");
}
Console.Write("*");
}
for(j = 1; j <= mid - i; j++) //print the blank spaces and outer box after star
Console.Write(" ");
Console.WriteLine();
}
// 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
Console.Write(" ");
if(i == n - 1) {
Console.Write("*");
}else{
Console.Write("*"); //in each line star at start and end position
for(j = 1; j <= 2*(n - i)-3; j++) { //print space to make hollow
Console.Write(" ");
}
Console.Write("*");
}
for(j = 1; j<=i-mid; j++) //print the blank spaces and outer box after star
Console.Write(" ");
Console.WriteLine();
}
}
// driver's code
public static void Main()
{
int n = 7;
printPattern(n);
}
}
// This code is contributed by Aman Kumar.
输出:
*
* *
* *
* *
* *
* *
*
时间复杂度:给定输入 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 处关闭循环。
using System;
namespace Pattern
{
class Program
{
static void PrintPattern(ref 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)
Console.Write("-");
else if (j == 1)
Console.Write("|");
else Console.Write(" ");
}
if (i == 1)
{
Console.Write("*");
}
else
{
Console.Write("*"); //in each line star at start and end position
for (j = 1; j <= 2 * i - 3; j++)
{ //print space to make hollow
Console.Write(" ");
}
Console.Write("*");
}
for (j = 1; j <= mid - i; j++)
{ //print the blank spaces and outer box after star
if (i == 1)
Console.Write("-");
else if (j == mid - i)
Console.Write("|");
else Console.Write(" ");
}
Console.WriteLine();
}
// 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)
Console.Write("-");
else if (j == 1)
Console.Write("|");
else Console.Write(" ");
}
if (i == n - 1)
{
Console.Write("*");
}
else
{
Console.Write("*"); //in each line star at start and end position
for (j = 1; j <= 2 * (n - i) - 3; j++)
{ //print space to make hollow
Console.Write(" ");
}
Console.Write("*");
}
for (j = 1; j <= i - mid; j++)
{ //print the blank spaces and outer box after star
if (i == n - 1)
Console.Write("-");
else if (j == i - mid)
Console.Write("|");
else Console.Write(" ");
}
Console.WriteLine();
}
}
static void Main(string[] args)
{
int n = 12;
PrintPattern(ref n);
}
}
}
// This code is contributed by factworx412
输出:
-----*-----
| * * |
| * * |
| * * |
|* *|
* *
|* *|
| * * |
| * * |
| * * |
-----*-----

时间复杂度: O(n*n)
辅助空间: O(1)
C# 打印空心图案
3870

被折叠的 条评论
为什么被折叠?



