原文:

给定一个数字 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   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

python 3

# 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

java 描述语言


output: 

\*******/
*\*****/*
**\***/**
***\*/***
****/****
***/*\***
**/***\**
*/*****\*
/*******\