📜  打印鱼形图案的程序

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

打印鱼形图案的程序

给定一个整数N ,任务是打印超过 2N+1 行的鱼图案。

例子:

做法:鱼由三部分组成:

  • 上半部分:超过 N 行。
  • 中间部分:中间单排
  • 下部:N行以上

现在,让我们尝试使用一个示例来理解该模式:
对于 N=3,鱼是:

现在,要解决这个问题,请按照以下步骤操作:

  1. 首先,创建上部:
    • 运行i=0 到 i的循环,并在循环的每次迭代中:
      • 如上图所示,首先出现一个字符串,比如有M (最初M=N )个空格的spaces1 ,然后是一层星星,比如说stars1只有 1 个星星,然后字符串spaces1出现 2 次(有2*M空间),然后是另一层星星,比如stars2最初有 0 颗星星。
      • 现在在每次迭代中, spaces1减少了一个空格, stars1增加了 2 颗星, stars2增加了 1 颗星。
  2. 对于中间部分,只需打印stars1stars2 ,因为此行中没有空格。
  3. 现在,为了得到下半部分,反转上半部分的算法。
  4. 循环结束后,将创建鱼的图案。

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print the pattern of a fish
// over N rows
void printFish(int N)
{
 
    string spaces1 = "", spaces2 = "";
    string stars1 = "*", stars2 = "";
    for (int i = 0; i < N; ++i) {
        spaces1 += ' ';
    }
    spaces2 = spaces1;
 
    for (int i = 0; i < 2 * N + 1; ++i) {
        // For upper part
        if (i < N) {
            cout << spaces1 << stars1
                 << spaces1 << spaces1
                 << stars2 << endl;
            spaces1.pop_back();
            stars1 += "**";
            stars2 += "*";
        }
 
        // For middle part
        if (i == N) {
            cout << spaces1 << stars1
                 << spaces1 << spaces1
                 << stars2 << endl;
        }
 
        // For lower part
        if (i > N) {
            spaces1 += ' ';
            stars1.pop_back();
            stars1.pop_back();
            stars2.pop_back();
            cout << spaces1 << stars1
                 << spaces1 << spaces1
                 << stars2 << endl;
        }
    }
}
 
// Driver Code
int main()
{
    int N = 5;
    printFish(N);
}


Java
// Java program for the above approach
class GFG {
 
    // Function to print the pattern of a fish
    // over N rows
    public static void printFish(int N) {
 
        String spaces1 = "";
        String stars1 = "*", stars2 = "";
        for (int i = 0; i < N; ++i) {
            spaces1 += ' ';
        }
 
        for (int i = 0; i < 2 * N + 1; ++i)
        {
           
            // For upper part
            if (i < N) {
                System.out.print(spaces1 + stars1 + spaces1 + spaces1);
                System.out.println(stars2);
                spaces1 = spaces1.substring(0, spaces1.length() - 1);
                stars1 += "**";
                stars2 += "*";
            }
 
            // For middle part
            if (i == N) {
                System.out.print(spaces1 + stars1 + spaces1 + spaces1);
                System.out.println(stars2);
            }
 
            // For lower part
            if (i > N) {
                spaces1 += ' ';
                stars1 = stars1.substring(0, stars1.length() - 1);
                stars1 = stars1.substring(0, stars1.length() - 1);
                stars2 = stars2.substring(0, stars2.length() - 1);
                System.out.print(spaces1 + stars1 + spaces1 + spaces1);
                System.out.println(stars2);
            }
        }
    }
 
    // Driver Code
    public static void main(String args[]) {
        int N = 5;
        printFish(N);
    }
}
 
// This code is contributed by gfgking.


Python3
# Python3 program for the above approach
 
# Function to print the pattern of a fish
# over N rows
def printFish(N) :
 
    spaces1 = ""; spaces2 = "";
    stars1 = "*"; stars2 = "";
    for i in range(N) :
        spaces1 += ' ';
 
    spaces2 = spaces1;
 
    for i in range( 2 * N + 1) :
        # For upper part
        if (i < N) :
            print(spaces1,end="");
            print(stars1,end="");
            print(spaces1,end="");
            print(spaces1,end="");
            print(stars2);
            spaces1 = spaces1[:-1]
            stars1 += "**";
            stars2 += "*";
 
        # For middle part
        if (i == N) :
            print(spaces1,end="");
            print(stars1,end="");
            print(spaces1,end="");
            print(spaces1,end="");
            print(stars2);
 
        # For lower part
        if (i > N) :
            spaces1 += ' ';
            stars1 = stars1[:-1];
            stars1 = stars1[:-1];
            stars2 = stars2[:-1];
             
            print(spaces1,end="");
            print(stars1,end="")
            print(spaces1,end="");
            print(spaces1,end="");
            print(stars2);
  
# Driver Code
if __name__ == "__main__" :
 
    N = 5;
    printFish(N);
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
class GFG {
 
    // Function to print the pattern of a fish
    // over N rows
    static void printFish(int N) {
 
        string spaces1 = "";
        string stars1 = "*", stars2 = "";
        for (int i = 0; i < N; ++i) {
            spaces1 += ' ';
        }
 
        for (int i = 0; i < 2 * N + 1; ++i)
        {
           
            // For upper part
            if (i < N) {
                Console.Write(spaces1 + stars1 + spaces1 + spaces1);
                Console.Write(stars2 + "\n");
                spaces1 = spaces1.Substring(0, spaces1.Length - 1);
                stars1 += "**";
                stars2 += "*";
            }
 
            // For middle part
            if (i == N) {
                Console.Write(spaces1 + stars1 + spaces1 + spaces1);
                Console.Write(stars2 + "\n");
            }
 
            // For lower part
            if (i > N) {
                spaces1 += ' ';
                stars1 = stars1.Substring(0, stars1.Length - 1);
                stars1 = stars1.Substring(0, stars1.Length - 1);
                stars2 = stars2.Substring(0, stars2.Length - 1);
                Console.Write(spaces1 + stars1 + spaces1 + spaces1);
                Console.Write(stars2 + "\n");
            }
        }
    }
 
    // Driver Code
    public static void Main() {
        int N = 5;
        printFish(N);
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
*          
    ***        *
   *****      **
  *******    ***
 *********  ****
****************
 *********  ****
  *******    ***
   *****      **
    ***        *
     *          

时间复杂度: O(N)
辅助空间: O(N)