📜  山序模式

📅  最后修改于: 2021-04-22 10:24:12             🧑  作者: Mango

给定一个数字N ,任务是生成一个金字塔序列模式,该模式一个接一个地包含N个金字塔,如下例所示。
例子:

Input: N = 3
Output:
  *    *    *
 ***  ***  ***
***************

Input: N = 4
Output: 
  *    *    *    *
 ***  ***  ***  ***
********************

迭代方法:迭代方法的步骤,用于打印给定数字NMountain Sequence Pattern

  1. 运行两个嵌套循环。
  2. 循环将照顾模式的
  3. 循环将照顾模式的
  4. 取三个变量k1,k2gap ,这有助于生成模式。
  5. 在打印一行图案后,将k1k2的值更新为:
    • k1 = k1 +间隙
    • k2 = k2 +间隙

下面是迭代方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to create the mountain
// sequence pattern
void printPatt(int n)
{
    int k1 = 3;
    int k2 = 3;
    int gap = 5;
 
    // Outer loop to handle the row
    for (int i = 1; i <= 3; i++) {
 
        // Inner loop to handle the
        // Coloumn
        for (int j = 1;
             j <= (5 * n); j++) {
 
            if (j > k2 && i < 3) {
                k2 += gap;
                k1 += gap;
            }
 
            // Condition to print the
            // star in mountain pattern
            if (j >= k1 && j <= k2) {
                cout << "*";
            }
            else {
                cout << " ";
            }
        }
 
        // Condition to adjust the value of
        // K1 and K2 for printing desire
        // Pattern
        if (i + 1 == 3) {
            k1 = 1;
            k2 = (5 * n);
        }
        else {
            k1 = 3;
            k2 = 3;
            k1--;
            k2++;
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 5;
 
    // Function call
    printPatt(N);
}


Java
// Java implementation of the above approach
class GFG{
 
// Function to create the mountain
// sequence pattern
static void printPatt(int n)
{
    int k1 = 3;
    int k2 = 3;
    int gap = 5;
 
    // Outer loop to handle the row
    for(int i = 1; i <= 3; i++)
    {
         
       // Inner loop to handle the
       // Coloumn
       for(int j = 1; j <= (5 * n); j++)
       {
          if (j > k2 && i < 3)
          {
              k2 += gap;
              k1 += gap;
          }
           
          // Condition to print the
          // star in mountain pattern
          if (j >= k1 && j <= k2)
          {
              System.out.print("*");
          }
          else
          {
              System.out.print(" ");
          }
       }
        
       // Condition to adjust the value of
       // K1 and K2 for printing desire
       // Pattern
       if (i + 1 == 3)
       {
           k1 = 1;
           k2 = (5 * n);
       }
       else
       {
           k1 = 3;
           k2 = 3;
           k1--;
           k2++;
       }
       System.out.println();
    }
}
     
// Driver code
public static void main (String[] args)
{
     
    // Given Number N
    int N = 5;
 
    // Function call
    printPatt(N);
}
}
 
// This code is contributed by Pratima Pandey


Python3
# Python3 program for the above approach
 
# Function to create the mountain
# sequence pattern
def printPatt(n):
 
    k1 = 3; k2 = 3; gap = 5;
 
    # Outer loop to handle the row
    for i in range(1, 4):
 
        # Inner loop to handle the
        # Coloumn
        for j in range(1, (5 * n) + 1):
 
            if (j > k2 and i < 3):
                k2 += gap;
                k1 += gap;
             
            # Condition to print the
            # star in mountain pattern
            if (j >= k1 and j <= k2):
                print("*", end = "");
            else:
                print(" ", end = "");
        print("\n", end = "");
             
        # Condition to adjust the value of
        # K1 and K2 for printing desire
        # Pattern
        if (i + 1 == 3):
            k1 = 1;
            k2 = (5 * n);
         
        else:
            k1 = 3;
            k2 = 3;
            k1 -= 1;
            k2 += 1;
    print(end = "");
     
# Driver Code
 
# Given Number N
N = 5;
 
# Function call
printPatt(N);
 
# This code is contributed by Code_Mech


C#
// C# implementation of the above approach
using System;
class GFG{
 
// Function to create the mountain
// sequence pattern
static void printPatt(int n)
{
    int k1 = 3;
    int k2 = 3;
    int gap = 5;
 
    // Outer loop to handle the row
    for(int i = 1; i <= 3; i++)
    {
         
        // Inner loop to handle the
        // Coloumn
        for(int j = 1; j <= (5 * n); j++)
        {
            if (j > k2 && i < 3)
            {
                k2 += gap;
                k1 += gap;
            }
             
            // Condition to print the
            // star in mountain pattern
            if (j >= k1 && j <= k2)
            {
                Console.Write("*");
            }
            else
            {
                Console.Write(" ");
            }
        }
             
        // Condition to adjust the value of
        // K1 and K2 for printing desire
        // Pattern
        if (i + 1 == 3)
        {
            k1 = 1;
            k2 = (5 * n);
        }
        else
        {
            k1 = 3;
            k2 = 3;
            k1--;
            k2++;
        }
        Console.WriteLine();
    }
}
     
// Driver code
public static void Main (String[] args)
{
     
    // Given Number N
    int N = 5;
 
    // Function call
    printPatt(N);
}
}
 
// This code is contributed by shivanisinghss2110


C++
// C++ program for the above approach
 
#include 
using namespace std;
int k1 = 2;
int k2 = 2;
int gap = 5;
 
// Function to print pattern
// recursively
int printPattern(
    int i, int j, int n)
{
 
    // Base Case
    if (j >= n) {
        k1 = 2;
        k2 = 2;
        k1--;
        k2++;
        if (i == 2) {
            k1 = 0;
            k2 = n - 1;
        }
        return 0;
    }
 
    // Condition to check row limit
    if (i >= 3) {
        return 1;
    }
 
    // Condition for assigning gaps
    if (j > k2) {
        k1 += gap;
        k2 += gap;
    }
 
    // Conditions to print *
    if (j >= k1
            && j <= k2
        || i == 2) {
 
        cout << "*";
    }
 
    // Else print ' '
    else {
 
        cout << " ";
    }
 
    // Recursive call for columns
    if (printPattern(i, j + 1, n)
        == 1) {
        return 1;
    }
 
    cout << endl;
 
    // Recursive call for rows
    return printPattern(i + 1,
                        0, n);
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 3;
 
    // Function Call
    printPattern(0, 0, N * 5);
    return 0;
}


Java
// Java program for the
// above approach
class GFG{
 
static int k1 = 2;
static int k2 = 2;
static int gap = 5;
     
// Function to print pattern
// recursively
public static int printPattern(int i,
                               int j,
                               int n)
{      
  // Base Case
  if (j >= n)
  {
    k1 = 2;
    k2 = 2;
    k1--;
    k2++;
    if (i == 2)
    {
      k1 = 0;
      k2 = n - 1;
    }
    return 0;
  }
 
  // Condition to check
  // row limit
  if (i >= 3)
  {
    return 1;
  }
 
  // Condition for assigning gaps
  if (j > k2)
  {
    k1 += gap;
    k2 += gap;
  }
 
  // Conditions to print *
  if (j >= k1 && j <= k2 || i == 2)
  {
    System.out.print("*");
  }
 
  // Else print ' '
  else
  {
    System.out.print(" ");
  }
 
  // Recursive call for columns
  if (printPattern(i, j + 1, n) == 1)
  {
    return 1;
  }
 
  System.out.println();
 
  // Recursive call for rows
  return printPattern(i + 1, 0, n);
}
 
// Driver code
public static void main(String[] args)
{
  // Given Number N
  int N = 3;
 
  // Function Call
  printPattern(0, 0, N * 5);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the
# above approach
k1 = 2
k2 = 2
gap = 5
 
# Function to print pattern
# recursively
def printPattern(i, j, n):
   
    global k1
    global k2
    global gap
 
    # Base Case
    if(j >= n):
        k1 = 2
        k2 = 2
        k1 -= 1
        k2 += 1
        if(i == 2):
            k1 = 0
            k2 = n - 1
        return 0
 
    # Condition to check row limit
    if(i >= 3):
        return 1
 
    # Condition for assigning gaps
    if(j > k2):
        k1 += gap
        k2 += gap
 
    # Conditions to print *
    if(j >= k1 and j <= k2 or
       i == 2):
        print("*", end = "")
 
    # Else print ' '
    else:
        print(" ", end = "")
 
    # Recursive call for columns
    if(printPattern(i, j + 1, n) == 1):
        return 1
 
    print()
 
    # Recursive call for rows
    return (printPattern(i + 1, 0, n))
 
# Driver Code
 
# Given Number N
N = 3
 
# Function Call
printPattern(0, 0, N * 5)
 
#This code is contributed by avanitrachhadiya2155


C#
// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG {
     
    static int k1 = 2;
    static int k2 = 2;
    static int gap = 5;
 
    // Function to print pattern
    // recursively
    static int printPattern(int i, int j, int n)
    {      
      // Base Case
      if (j >= n)
      {
        k1 = 2;
        k2 = 2;
        k1--;
        k2++;
        if (i == 2)
        {
          k1 = 0;
          k2 = n - 1;
        }
        return 0;
      }
      
      // Condition to check
      // row limit
      if (i >= 3)
      {
        return 1;
      }
      
      // Condition for assigning gaps
      if (j > k2)
      {
        k1 += gap;
        k2 += gap;
      }
      
      // Conditions to print *
      if (j >= k1 && j <= k2 || i == 2)
      {
        Console.Write("*");
      }
      
      // Else print ' '
      else
      {
        Console.Write(" ");
      }
      
      // Recursive call for columns
      if (printPattern(i, j + 1, n) == 1)
      {
        return 1;
      }
      
      Console.WriteLine();
      
      // Recursive call for rows
      return printPattern(i + 1, 0, n);
    } 
   
  // Driver code
  static void Main()
  {
     
      // Given Number N
      int N = 3;
      
      // Function Call
      printPattern(0, 0, N * 5);
  }
}
 
// This code is contributed by divyeshrabadiya07


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

时间复杂度: O(N)
递归方法:可以使用递归生成模式。步骤如下:

  1. 运行两个嵌套循环。
  2. 循环将照顾模式的
  3. 循环将照顾模式的
  4. 除此之外,还需要变量K1K2gap
  5. K1,K2将涵盖要打印*的情况。
  6. 间隙将覆盖要打印空格的情况。
  7. 递归调用fun(i,j + 1)函数来处理列。
  8. 递归调用函数fun(i + 1,0)处理行。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
int k1 = 2;
int k2 = 2;
int gap = 5;
 
// Function to print pattern
// recursively
int printPattern(
    int i, int j, int n)
{
 
    // Base Case
    if (j >= n) {
        k1 = 2;
        k2 = 2;
        k1--;
        k2++;
        if (i == 2) {
            k1 = 0;
            k2 = n - 1;
        }
        return 0;
    }
 
    // Condition to check row limit
    if (i >= 3) {
        return 1;
    }
 
    // Condition for assigning gaps
    if (j > k2) {
        k1 += gap;
        k2 += gap;
    }
 
    // Conditions to print *
    if (j >= k1
            && j <= k2
        || i == 2) {
 
        cout << "*";
    }
 
    // Else print ' '
    else {
 
        cout << " ";
    }
 
    // Recursive call for columns
    if (printPattern(i, j + 1, n)
        == 1) {
        return 1;
    }
 
    cout << endl;
 
    // Recursive call for rows
    return printPattern(i + 1,
                        0, n);
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 3;
 
    // Function Call
    printPattern(0, 0, N * 5);
    return 0;
}

Java

// Java program for the
// above approach
class GFG{
 
static int k1 = 2;
static int k2 = 2;
static int gap = 5;
     
// Function to print pattern
// recursively
public static int printPattern(int i,
                               int j,
                               int n)
{      
  // Base Case
  if (j >= n)
  {
    k1 = 2;
    k2 = 2;
    k1--;
    k2++;
    if (i == 2)
    {
      k1 = 0;
      k2 = n - 1;
    }
    return 0;
  }
 
  // Condition to check
  // row limit
  if (i >= 3)
  {
    return 1;
  }
 
  // Condition for assigning gaps
  if (j > k2)
  {
    k1 += gap;
    k2 += gap;
  }
 
  // Conditions to print *
  if (j >= k1 && j <= k2 || i == 2)
  {
    System.out.print("*");
  }
 
  // Else print ' '
  else
  {
    System.out.print(" ");
  }
 
  // Recursive call for columns
  if (printPattern(i, j + 1, n) == 1)
  {
    return 1;
  }
 
  System.out.println();
 
  // Recursive call for rows
  return printPattern(i + 1, 0, n);
}
 
// Driver code
public static void main(String[] args)
{
  // Given Number N
  int N = 3;
 
  // Function Call
  printPattern(0, 0, N * 5);
}
}
 
// This code is contributed by divyeshrabadiya07

Python3

# Python3 program for the
# above approach
k1 = 2
k2 = 2
gap = 5
 
# Function to print pattern
# recursively
def printPattern(i, j, n):
   
    global k1
    global k2
    global gap
 
    # Base Case
    if(j >= n):
        k1 = 2
        k2 = 2
        k1 -= 1
        k2 += 1
        if(i == 2):
            k1 = 0
            k2 = n - 1
        return 0
 
    # Condition to check row limit
    if(i >= 3):
        return 1
 
    # Condition for assigning gaps
    if(j > k2):
        k1 += gap
        k2 += gap
 
    # Conditions to print *
    if(j >= k1 and j <= k2 or
       i == 2):
        print("*", end = "")
 
    # Else print ' '
    else:
        print(" ", end = "")
 
    # Recursive call for columns
    if(printPattern(i, j + 1, n) == 1):
        return 1
 
    print()
 
    # Recursive call for rows
    return (printPattern(i + 1, 0, n))
 
# Driver Code
 
# Given Number N
N = 3
 
# Function Call
printPattern(0, 0, N * 5)
 
#This code is contributed by avanitrachhadiya2155

C#

// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG {
     
    static int k1 = 2;
    static int k2 = 2;
    static int gap = 5;
 
    // Function to print pattern
    // recursively
    static int printPattern(int i, int j, int n)
    {      
      // Base Case
      if (j >= n)
      {
        k1 = 2;
        k2 = 2;
        k1--;
        k2++;
        if (i == 2)
        {
          k1 = 0;
          k2 = n - 1;
        }
        return 0;
      }
      
      // Condition to check
      // row limit
      if (i >= 3)
      {
        return 1;
      }
      
      // Condition for assigning gaps
      if (j > k2)
      {
        k1 += gap;
        k2 += gap;
      }
      
      // Conditions to print *
      if (j >= k1 && j <= k2 || i == 2)
      {
        Console.Write("*");
      }
      
      // Else print ' '
      else
      {
        Console.Write(" ");
      }
      
      // Recursive call for columns
      if (printPattern(i, j + 1, n) == 1)
      {
        return 1;
      }
      
      Console.WriteLine();
      
      // Recursive call for rows
      return printPattern(i + 1, 0, n);
    } 
   
  // Driver code
  static void Main()
  {
     
      // Given Number N
      int N = 3;
      
      // Function Call
      printPattern(0, 0, N * 5);
  }
}
 
// This code is contributed by divyeshrabadiya07
输出:
*    *    *  
 ***  ***  *** 
***************

时间复杂度: O(N)