📜  打印图案“G”的程序

📅  最后修改于: 2022-05-13 01:56:56.481000             🧑  作者: Mango

打印图案“G”的程序

在本文中,我们将学习如何使用星号和空格打印图案 G。给定一个数字 n,我们将编写一个程序在 n 行或行上打印模式 G。
例子:

Input : 7
Output :
  ***  
 *     
 *     
 * *** 
 *   * 
 *   * 
  ***  

Input : 9
Output :
  *****  
 *       
 *       
 *       
 *   *** 
 *     * 
 *     * 
 *     * 
  *****  

在这个程序中,我们使用了简单的线迭代逻辑来创建模式 G。请看下图,它以二维矩阵的形式表示模式 G,其中 mat[i][j] = 'j':

G

如果我们尝试用(行,列)矩阵分析这张图片,圆圈代表图案 G 中星星的位置,我们将学习这些步骤。在这里,我们按列执行操作。所以对于第一行星星,我们设置第一个if条件,其中0和(n-1)的行位置不会得到星星,而从1到(n-1)的所有其他行都会得到星星.同样,对于第二、第三和第四列,我们希望在 row = 0 和 row = (n-1) 的位置有星星。其他步骤是不言自明的,可以从图中的行和列的位置来理解。
下面是上述想法的实现:

C++
// C++ program to print the pattern G
#include 
using namespace std;
 
void pattern(int line)
{
    int i, j;
    for(i = 0; i < line; i++)
    {
        for(j = 0; j < line; j++)
        {
            if((j == 1 && i != 0 && i != line - 1) ||
              ((i == 0 || i == line - 1) && j > 1 &&
              j < line - 2) || (i == ((line - 1) / 2) &&
              j > 2 && j < line - 1) || (j == line - 2 &&
              i != 0 && i >= ((line - 1) / 2) && i != line - 1))
                printf("*");
            else
                printf( " ");
 
        }
        printf("\n");
    }
}
 
// Driver code
int main()
{
    int line = 7;
    pattern(line);
    return 0;
}
 
// This code is contributed
// by  vt_m.


Java
// Java program to print the pattern G
import java.io.*;
 
class GFG {
 
    static void pattern(int line)
    {
        int i, j;
        for(i = 0; i < line; i++)
        {
            for(j = 0; j < line; j++)
            {
                if((j == 1 && i != 0 && i != line - 1) ||
                ((i == 0 || i == line - 1) && j > 1 &&
                j < line - 2) || (i == ((line - 1) / 2) &&
                j > 2 && j < line - 1) || (j == line - 2 &&
                i != 0 && i >= ((line - 1) / 2) && i != line - 1))
                    System.out.print("*");
                else
                    System.out.print( " ");
     
            }
            System.out.println();
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int line = 7;
        pattern(line);
    }
}
 
// This code is contributed by vt_m.


Python
# Python program to print pattern G
def Pattern(line):
    pat=""
    for i in range(0,line):   
        for j in range(0,line):    
            if ((j == 1 and i != 0 and i != line-1) or ((i == 0 or
                i == line-1) and j > 1 and j < line-2) or (i == ((line-1)/2)
                and j > line-5 and j < line-1) or (j == line-2 and
                i != 0 and i != line-1 and i >=((line-1)/2))): 
                pat=pat+"*"  
            else:     
                pat=pat+" "  
        pat=pat+"\n"  
    return pat
  
# Driver Code
line = 7
print(Pattern(line))


C#
// C# program to print the pattern G
using System;
 
class GFG {
 
    static void pattern(int line)
    {
        int i, j;
        for(i = 0; i < line; i++)
        {
            for(j = 0; j < line; j++)
            {
                if((j == 1 && i != 0 && i != line - 1) ||
                ((i == 0 || i == line - 1) && j > 1 &&
                j < line - 2) || (i == ((line - 1) / 2) &&
                j > 2 && j < line - 1) || (j == line - 2 &&
                i != 0 && i >= ((line - 1) / 2) && i != line - 1))
                    Console.Write("*");
                else
                    Console.Write( " ");
     
            }
            Console.WriteLine();
        }
    }
     
    // Driver code
    public static void Main ()
    {
        int line = 7;
        pattern(line);
    }
}
 
// This code is contributed by vt_m.


PHP
 1 and $j < $line-2) or ($i == (($line-1)/2)
            and $j > 2 and $j < $line-1) or ($j == $line-2 and
            $i != 0 and $i >=(($line-1)/2) and $i != $line-1))
                echo "*";   
            else
                echo " ";    
        }       
      echo "\n";
    }
}
 
// Driver Code
$line = 7;
Pattern($line)
?>


Javascript


输出:

***  
 *     
 *     
 * *** 
 *   * 
 *   * 
  ***