给定数字N (≥8),任务是在“三角形”图案内打印“空心三角形”。
例子:
Input: N = 9
Output:
*
* *
* *
* * *
* * * * *
* *
* *
* *
* * * * * * * * * * * * * * * * *
方法:
设i为行的索引, j为列的索引。然后:
- 对于外部三角形的边:
如果列( j )的索引等于( N – i + 1 )或( N + i – 1 ),则为外三角形的等边打印‘*’ 。
if(j == (N - i + 1)
|| j == (N + i - 1) {
print('*')
}
- 对于内三角形的边:
如果(第( i )行的索引小于( N – 4 )且大于( 4 ),而第( j )列的索引等于( N – i + 4 )或( N + i + 4 ),则内三角形的等边印有“ *” 。
if( (i >= 4
&& i <= n - 4)
&& (j == N - i + 4
|| j == N + i - 4) ) {
print('*')
}
- 对于外部三角形的底面:
如果row( i )的索引等于N ,那么将为外部三角形的底数打印“ *” 。
if(i == N) {
print('*')
}
- 对于内部三角形的底面:
如果row( i )的索引等于(N – 4) ,并且列index( j )必须大于等于( N –(N – 2 * 4) ),并且j小于等于( N + N – 2 * 4 ),然后为内部三角形的底数打印‘*’ 。
if( (i == N - 4)
&& (j >= N - (N - 2 * 4) )
&& (j <= n + n - 2 * 4) ) ) {
print('*')
}
下面是上述方法的实现:
CPP
// C++ implementation of the above approach
#include
using namespace std;
// Function to print the pattern
void printPattern(int n)
{
int i, j;
// Loop for rows
for (i = 1; i <= n; i++) {
// Loop for column
for (j = 1; j < 2 * n; j++) {
// For printing equal sides
// of outer triangle
if (j == (n - i + 1)
|| j == (n + i - 1)) {
cout << "* ";
}
// For printing equal sides
// of inner triangle
else if ((i >= 4 && i <= n - 4)
&& (j == n - i + 4
|| j == n + i - 4)) {
cout << "* ";
}
// For printing base
// of both triangle
else if (i == n
|| (i == n - 4
&& j >= n - (n - 2 * 4)
&& j <= n + n - 2 * 4)) {
cout << "* ";
}
// For spacing between the triangle
else {
cout << " "
<< " ";
}
}
cout << "\n";
}
}
// Driver Code
int main()
{
int N = 9;
printPattern(N);
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG{
// Function to print the pattern
static void printPattern(int n)
{
int i, j;
// Loop for rows
for (i = 1; i <= n; i++) {
// Loop for column
for (j = 1; j < 2 * n; j++) {
// For printing equal sides
// of outer triangle
if (j == (n - i + 1)
|| j == (n + i - 1)) {
System.out.print("* ");
}
// For printing equal sides
// of inner triangle
else if ((i >= 4 && i <= n - 4)
&& (j == n - i + 4
|| j == n + i - 4)) {
System.out.print("* ");
}
// For printing base
// of both triangle
else if (i == n
|| (i == n - 4
&& j >= n - (n - 2 * 4)
&& j <= n + n - 2 * 4)) {
System.out.print("* ");
}
// For spacing between the triangle
else {
System.out.print(" "
+ " ");
}
}
System.out.print("\n");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 9;
printPattern(N);
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 implementation of the above approach
# Function to print the pattern
def printPattern(n):
# Loop for rows
for i in range(1, n + 1):
# Loop for column
for j in range(1, 2 * n):
# For printing equal sides
# of outer triangle
if (j == (n - i + 1)
or j == (n + i - 1)):
print("* ",end="")
# For printing equal sides
# of inner triangle
elif ((i >= 4 and i <= n - 4)
and (j == n - i + 4
or j == n + i - 4)):
print("* ",end="")
# For printing base
# of both triangle
elif (i == n
or (i == n - 4
and j >= n - (n - 2 * 4)
and j <= n + n - 2 * 4)):
print("* ", end="")
# For spacing between the triangle
else :
print(" "+" ", end="")
print()
# Driver Code
N = 9
printPattern(N)
# This code is contributed by mohit kumar 29
C#
// C# implementation of the above approach
using System;
class GFG{
// Function to print the pattern
static void printPattern(int n)
{
int i, j;
// Loop for rows
for (i = 1; i <= n; i++) {
// Loop for column
for (j = 1; j < 2 * n; j++) {
// For printing equal sides
// of outer triangle
if (j == (n - i + 1)
|| j == (n + i - 1)) {
Console.Write("* ");
}
// For printing equal sides
// of inner triangle
else if ((i >= 4 && i <= n - 4)
&& (j == n - i + 4
|| j == n + i - 4)) {
Console.Write("* ");
}
// For printing base
// of both triangle
else if (i == n
|| (i == n - 4
&& j >= n - (n - 2 * 4)
&& j <= n + n - 2 * 4)) {
Console.Write("* ");
}
// For spacing between the triangle
else {
Console.Write(" "
+ " ");
}
}
Console.Write("\n");
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 9;
printPattern(N);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
*
* *
* *
* * *
* * * * *
* *
* *
* *
* * * * * * * * * * * * * * * * *