本文概述
- C ++
- Java
- Python 3
- C#
- 的PHP
- C ++
- Java
- Python 3
- C#
- 的PHP
例子:
Input : 7
Output :
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
算法:-
步骤1:-首先考虑基本条件, 即数字小于0
步骤2:-进行递归调用直到数字小于0, 即:-printPartten(n-1, k + 1);
步骤3:打印空格
步骤4:-然后列印*直到编号
下面是上述方法的实现:
C ++
//C++ program to print triangular patterns using Recursive
#include <
iostream>
using namespace std;
void printPartten( int n, int k)
{
if (n <
0) //Base condition
return ;
//Recursive call
printPartten(n - 1, k + 1);
int i;
for (i = 0;
i <
k;
i++) //it makes spaces
cout <
<
" " ;
for (i = 0;
i <
n;
i++) //for print *
printf ( "* " );
printf ( "\n" );
//for next line
}int main()
{
int n = 7;
//Call to printPartten function
printPartten(n, 0);
return 0;
}
Java
//Java program to print triangular patterns using Recursive
class GFG{
static void printPartten( int n, int k)
{
if (n <
0 ) //Base condition
return ;
//Recursive call
printPartten(n - 1 , k + 1 );
int i;
for (i = 0 ;
i <
k;
i++) //it makes spaces
System.out.printf( " " );
for (i = 0 ;
i <
n;
i++) //for print *
System.out.printf( "* " );
System.out.printf( "\n" );
//for next line
} public static void main(String[]args)
{
int n = 7 ;
//Call to printPartten function
printPartten(n, 0 );
}
}
Python 3
# Python 3 program to print triangular
# patterns using Recursivedef printPartten(n, k):if (n <
0 ): # Base condition
return ;
# Recursive call
printPartten(n - 1 , k + 1 );
for i in range ( 0 , k): # it makes spaces
print ( " " , end = "");
for i in range ( 0 , n): # for print *
print ( "* " , end = "");
print ( "\n" , end = "");
# for next line# Driver Code
n = 7 ;
# Call to printPartten function
printPartten(n, 0 );
# This code is contributed
# by Akanksha Rai
C#
//C# program to print triangular
//patterns using Recursive
using System;
class GFG
{
static void printPartten( int n, int k)
{
if (n <
0) //Base condition
return ;
//Recursive call
printPartten(n - 1, k + 1);
int i;
for (i = 0;
i <
k;
i++) //it makes spaces
Console.Write( " " );
for (i = 0;
i <
n;
i++) //for print *
Console.Write( "* " );
Console.Write( "\n" );
//for next line
} //Driver Code
public static void Main()
{
int n = 7;
//Call to printPartten function
printPartten(n, 0);
}
} //This code is contributed
//by Subhadeep
的PHP
<
?php
//PHP program to print triangular
//patterns using Recursive function printPartten( $n , $k )
{
if ( $n <
0) //Base condition
return ;
//Recursive call
printPartten( $n - 1, $k + 1);
for ( $i = 0;
$i <
$k ;
$i ++) //it makes spaces
echo " " ;
for ( $i = 0;
$i <
$n ;
$i ++) //for print *
echo ( "* " );
echo ( "\n" );
//for next line
} //Driver Code
$n = 7;
//Call to printPartten function
printPartten( $n , 0);
//This code is contributed by jit_t
?>
输出如下:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
如何打印其反向图案?
只需将递归行放在函数的末尾
例子:
Input : 7
Output :
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
C ++
//C++ program to print reverse triangular
//patterns using Recursive
#include <
iostream>
using namespace std;
void printPartten( int n, int k)
{
if (n <
0) //Base condition
return ;
int i;
for (i = 0;
i <
k;
i++) //it makes spaces
cout <
<
" " ;
for (i = 0;
i <
n;
i++) //for print *
printf ( "* " );
printf ( "\n" );
//for next line//Recursive calls
printPartten(n - 1, k + 1);
}int main()
{
int n = 7;
//Call to printPartten function
printPartten(n, 0);
return 0;
}
Java
//Java program to print reverse triangular
//patterns using Recursive
class GFG{
static void printPartten( int n, int k)
{
if (n <
0 ) //Base condition
return ;
int i;
for (i = 0 ;
i <
k;
i++) //it makes spaces
System.out.print( " " );
for (i = 0 ;
i <
n;
i++) //for print *
System.out.print( "* " );
System.out.print( "\n" );
//for next line //Recursive calls
printPartten(n - 1 , k + 1 );
} public static void main(String[] args)
{
int n = 7 ;
//Call to printPartten function
printPartten(n, 0 );
}
}
Python 3
# Python 3 program to print reverse
# triangular patterns using Recursivedef printPartten(n, k):if (n <
0 ): # Base condition
return ;
for i in range ( 0 , k): # it makes spaces
print ( " " , end = "")
for i in range ( 0 , n): # for print *
print ( "*" , end = " " )
print ( "\n" , end = "") # for next line# Recursive calls
printPartten(n - 1 , k + 1 );
# Driver Code
n = 7 ;
# Call to printPartten function
printPartten(n, 0 );
# This code is contributed
# by Akanksha Rai
C#
//C# program to print reverse triangular
//patterns using Recursive
using System;
class GFG
{
static void printPartten( int n, int k)
{
if (n <
0) //Base condition
return ;
int i;
for (i = 0;
i <
k;
i++) //it makes spaces
Console.Write( " " );
for (i = 0;
i <
n;
i++) //for print *
Console.Write( "* " );
Console.Write( "\n" );
//for next line //Recursive calls
printPartten(n - 1, k + 1);
} //Driver Code
public static void Main()
{
int n = 7;
//Call to printPartten function
printPartten(n, 0);
}
} //This code is contributed
//by PrinciRaj1992
的PHP
<
?php
//PHP program to print reverse triangular
//patterns using Recursive
function printPartten( $n , $k )
{
if ( $n <
0) //Base condition
return ;
for ( $i = 0;
$i <
$k ;
$i ++) //it makes spaces
echo ( " " );
for ( $i = 0;
$i <
$n ;
$i ++) //for print *
echo ( "* " );
echo ( "\n" );
//for next line //Recursive calls
printPartten( $n - 1, $k + 1);
} //Driver Code
$n = 7;
//Call to printPartten function
printPartten( $n , 0);
//This code is contributed
//by Mukul singh
?>
【算法题(递归程序打印三角形图案)】输出如下:
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
推荐阅读
- 埃森哲面试体验(校园内)
- 用所有其他的BitWise XOR替换数组的每个元素
- 算法设计(在给定大小的组中反向链表|S2)
- 生成长度为n的所有二进制字符串,其中子字符串“01”恰好出现两次
- 在R编程中从向量创建数据框
- GTX和RTX–哪个更好(GTX 1080Ti和RTX 2080有什么区别?)
- 算法题(求最长连续子序列)
- 算法题(检测并删除链表中的循环)
- 算法题(快速选择算法)