本文概述
给定一个数字ñ, 任务是打印三角形分隔的图案。
三角形分隔模式:其中四个三角形(左, 下, 右, 上)由正斜杠和后斜杠分隔的模式, 请参见以下内容:\ ***** / * \ *** / * ** \ * / * * *** / *** ** / * \ ** * / *** \ * / ***** \
注意: ñ应该是一个奇数, 并且值ñ应该大于4。
例子:
Input: N = 5
Output:
\***/
*\*/*
**/**
*/*\*
/***\
Input: N = 7
Output:
\*****/
*\***/*
**\*/**
***/***
**/*\**
*/***\*
/*****\
推荐:请尝试以下方法{IDE}首先, 在继续解决方案之前。
方法:通过观察上述模式, 当行和列的索引相等时, 则"打印, 并且当行和列的索引总和是ñ, 然后‘/’打印。下面是递归方法:
- 使用两个值一世用于行和Ĵ对于列, 该列从(0, 0)to(N-1, N-1)用于打印需求图案。
- 递归地从(0, 0)to(N-1, N-1):
- 基本情况:如果行和列的索引大于或等于ñ是给定模式的终止条件。
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 <bits/stdc++.h>
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
输出如下:
\*******/
*\*****/*
**\***/**
***\*/***
****/****
***/*\***
**/***\**
*/*****\*
/*******\