本文概述
- 建议:在继续解决方案之前, 请先在{IDE}上尝试使用你的方法。
- C / C ++
- Java
- Python3
- C#
三角形分隔模式:其中四个三角形(左, 下, 右, 上)由正斜杠和后斜杠分隔的模式, 请参见以下内容:\ ***** / * \ *** / * ** \ * / * * *** / *** ** / * \ ** * / *** \ * / ***** \注意: ?应该是一个奇数, 并且值?应该大于4。
例子:
Input: N = 5Output: \***/*\*/***/***/*\*/***\Input: N = 7Output:\*****/*\***/***\*/*****/*****/*\***/***\*/*****\
推荐:请尝试以下方法{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 rowsrecursive_function(i, j+1, N)// Recursive call for changing rowsrecursive_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 1print ()# 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
输出如下:
\*******/*\*****/***\***/*****\*/*******/*******/*\*****/***\***/*****\*/*******\
推荐阅读
- HTML如何使用标题(h标签用法代码示例)
- 如何使用jQuery选择文本节点()
- 云计算介绍和教程指南
- Python程序从列表中查找N个最大元素
- 亚马逊专题面试经验分析|S7
- Django中的视图如何使用(开发示例 | Python)
- Java中的锯齿数组如何使用(示例)
- u盘格式化后容量变小,本文教您u盘格式化后容量变小怎样恢复
- 制作u盘系统安装盘,本文教您如何制作u盘打开安装盘