📜  打印三角形分隔的图案

📅  最后修改于: 2021-04-26 19:31:18             🧑  作者: Mango

给定数字N ,任务是打印三角形分隔的图案。

注意: N应该是一个奇数,并且N的值应该大于4。

例子:

Input: N = 5
Output: 
        \***/
        *\*/*
        **/**
        */*\*
        /***\

Input: N = 7
Output:
        \*****/
        *\***/*
        **\*/**
        ***/***
        **/*\**
        */***\*
        /*****\

方法:通过观察上述模式,当行和列的索引相等时,将打印“” ,而当行和列的索引总和为N时,则打印“ /” 。下面是递归方法:

  1. 使用两个值i为行和j列,它迭代从(0,0)〜(N-1,N-1)用于打印需要的图案。
  2. (0,0)递归迭代到(N-1,N-1)
    • 基本情况:如果行和列的索引大于或等于N,则为给定模式的终止条件。
      if(i >= N) {
        return 0;
      }
      if(j >= N) {
        return 1;
      }
      
    • 打印语句:如果不满足基本条件,则根据以下条件打印“ /”“”“ *”
      if(i==j) {
         print('')
      }
      else if(i + j == N-1) {
         print('/')
      }
      else {
         print('*')
      }
      
    • 递归调用:在每个递归调用(基本情况除外)中,为行和列的下一次迭代返回递归函数:
      // Recursive call for rows
      recursive_function(i, j+1, N)
      
      // Recursive call for changing rows
      recursive_function(i+1, j, N)
      

下面是上述方法的实现:

C/C++
// C++ program to print the triangle
// separated pattern using
// star and slash character
  
#include 
using namespace std;
  
// Function to print pattern recursively
int printPattern(
    int i, int j, int n)
{
    // Base Case
    if (j >= n) {
        return 0;
    }
    if (i >= n) {
        return 1;
    }
  
    // Conditions to print slash
    if (j == i || j == n - 1 - i) {
  
        // Condition to print
        // forword slash
        if (i == n - 1 - j) {
            cout << "/";
        }
  
        // Condition to print
        // backward slash
        else {
            cout << "\\";
        }
    }
  
    // Else print '*'
    else {
        cout << "*";
    }
  
    // Recursive call for rows
    if (printPattern(i, j + 1, n)
        == 1) {
        return 1;
    }
  
    cout << endl;
  
    // Recursive call for changing
    // the rows
    return printPattern(i + 1, 0, n);
}
  
// Driver Code
int main()
{
    int N = 9;
  
    // Function Call
    printPattern(0, 0, N);
  
    return 0;
}


Java
// Java program to print the triangle
// separated pattern using
// star and slash character
class GFG{
   
// Function to print pattern recursively
static int printPattern(
    int i, int j, int n)
{
    // Base Case
    if (j >= n) {
        return 0;
    }
    if (i >= n) {
        return 1;
    }
   
    // Conditions to print slash
    if (j == i || j == n - 1 - i) {
   
        // Condition to print
        // forword slash
        if (i == n - 1 - j) {
            System.out.print("/");
        }
   
        // Condition to print
        // backward slash
        else {
            System.out.print("\\");
        }
    }
   
    // Else print '*'
    else {
        System.out.print("*");
    }
   
    // Recursive call for rows
    if (printPattern(i, j + 1, n)
        == 1) {
        return 1;
    }
   
    System.out.println();
   
    // Recursive call for changing
    // the rows
    return printPattern(i + 1, 0, n);
}
   
// Driver Code
public static void main(String[] args)
{
    int N = 9;
   
    // Function Call
    printPattern(0, 0, N);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python 3 program to print the triangle
# separated pattern using
# star and slash character
  
# Function to print pattern recursively
def printPattern(i,j, n):
  
    # Base Case
    if (j >= n) :
        return 0
    if (i >= n):
        return 1
  
    # Conditions to print slash
    if (j == i or j == n - 1 - i):
  
        # Condition to print
        # forword slash
        if (i == n - 1 - j):
            print("/",end="")
  
        # Condition to print
        # backward slash
        else:
            print("\\",end="")
  
    # Else print '*'
    else:
        print("*",end="")
  
    # Recursive call for rows
    if (printPattern(i, j + 1, n)
        == 1):
        return 1
  
    print()
  
    # Recursive call for changing
    # the rows
    return printPattern(i + 1, 0, n)
  
# Driver Code
if __name__ == "__main__":
  
    N = 9
  
    # Function Call
    printPattern(0, 0, N)
  
# This code is contributed by chitranayal


C#
// C# program to print the triangle
// separated pattern using
// star and slash character
using System;
  
class GFG{
    
// Function to print pattern recursively
static int printPattern(
    int i, int j, int n)
{
    // Base Case
    if (j >= n) {
        return 0;
    }
    if (i >= n) {
        return 1;
    }
    
    // Conditions to print slash
    if (j == i || j == n - 1 - i) {
    
        // Condition to print
        // forword slash
        if (i == n - 1 - j) {
            Console.Write("/");
        }
    
        // Condition to print
        // backward slash
        else {
            Console.Write("\\");
        }
    }
    
    // Else print '*'
    else {
        Console.Write("*");
    }
    
    // Recursive call for rows
    if (printPattern(i, j + 1, n)
        == 1) {
        return 1;
    }
    
    Console.WriteLine();
    
    // Recursive call for changing
    // the rows
    return printPattern(i + 1, 0, n);
}
    
// Driver Code
public static void Main(String[] args)
{
    int N = 9;
    
    // Function Call
    printPattern(0, 0, N);
}
}
  
// This code is contributed by Rajput-Ji


输出:
\*******/
*\*****/*
**\***/**
***\*/***
****/****
***/*\***
**/***\**
*/*****\*
/*******\