# 如何轻松学习图案打印？

2021年4月17日18:06:55 发表评论 739 次浏览

## 本文概述

``````Input: N = 4
Output:
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4

Input: N = 7
Output:
7 7 7 7 7 7 7 7 7 7 7 7 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 7 7 7 7 7 7 7 7 7 7 7 7``````

–在第一行中, 每个元素都是相同的, 即4(= N)。因此, 它没有太大帮助。

–在第二行中, 对于i> = j, 可以看到C从4减小到3, 然后对于下一个i = j, 它保持3, 然后对于所有j的值, 对于下一个i = j, 它保持2, 可以看到C从4减少到1。

``C(i, j) = N - min(i, j) + 1``

## C++

``````#include <iostream>
using namespace std;

int main()
{
int N = 4, i, j, min;
cout <<"Value of N: " <<N <<endl;

for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
min = i <j ? i : j;
cout <<N - min + 1;
}
cout <<endl;
}
return 0;
}``````

## Java

``````//Java program to find triplets
//a[i]>a[j]>a[k] and i<j<k
import java.util.*;

class GFG
{

//Driver code
public static void main(String[] args)
{
int N = 4 , i, j, min;
System.out.println( "Value of N: " + N);

for (i = 1 ; i <= N; i++)
{
for (j = 1 ; j <= N; j++)
{
min = i <j ? i : j;
System.out.print(N - min + 1 );
}
System.out.println();
}
}
}

//This code is contributed by Princi Singh``````

## Python3

``````# Python3 program to find triplets
# a[i]>a[j]>a[k] and i<j<k
if __name__ = = '__main__' :

N = 4 ;
print ( "Value of N: " , N);

for i in range ( 1 , N + 1 ):
for j in range ( 1 , N + 1 ):
min = i if i <j else j;
print (N - min + 1 , end = "");
print ();

# This code is contributed
# by PrinciRaj1992``````

## C#

``````//C# program to find triplets
//a[i]>a[j]>a[k] and i<j<k
using System;

class GFG
{

//Driver code
public static void Main(String[] args)
{
int N = 4, i, j, min;
Console.WriteLine( "Value of N: " + N);

for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
min = i <j ? i : j;
Console.Write(N - min + 1);
}
Console.WriteLine();
}
}
}

//This code is contributed by PrinciRaj1992``````

``````Value of N: 4
4444
4333
4322
4321``````

## C++

``````#include <iostream>
using namespace std;

int main()
{
int N = 4, i, j, min;
cout <<"Value of N: " <<N <<endl;

for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
min = i <j ? i : j;
cout <<N - min + 1;
}
for (j = N - 1; j>= 1; j--) {
min = i <j ? i : j;
cout <<N - min + 1;
}
cout <<endl;
}
return 0;
}``````

## Java

``````class GFG
{
public static void main(String[] args)
{
int N = 4 , i, j, min;
System.out.println( "Value of N: " + N);

for (i = 1 ; i <= N; i++)
{
for (j = 1 ; j <= N; j++)
{
min = i <j ? i : j;
System.out.print(N - min + 1 );
}
for (j = N - 1 ; j>= 1 ; j--)
{
min = i <j ? i : j;
System.out.print(N - min + 1 );
}
System.out.println();
}
}
}

//This code is contributed by PrinciRaj1992``````

## Python3

``````N = 4 ;
print ( "Value of N: " , N);

for i in range ( 1 , N + 1 ):
for j in range ( 1 , N + 1 ):
min = i if i <j else j;
print (N - min + 1 , end = "");

for j in range (N - 1 , 0 , - 1 ):
min = i if i <j else j;
print (N - min + 1 , end = "");
print ();

# This code is contributed by Rajput-Ji``````

## C#

``````using System;

class GFG
{
public static void Main(String[] args)
{
int N = 4, i, j, min;
Console.WriteLine( "Value of N: " + N);

for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
min = i <j ? i : j;
Console.Write(N - min + 1);
}
for (j = N - 1; j>= 1; j--)
{
min = i <j ? i : j;
Console.Write(N - min + 1);
}
Console.WriteLine();
}
}
}

//This code is contributed by Rajput-Ji``````

``````Value of N: 4
4444444
4333334
4322234
4321234``````

## C++

``````#include <iostream>
using namespace std;

int main()
{
int N = 4, i, j, min;
cout <<"Value of N: " <<N <<endl;

for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
min = i <j ? i : j;
cout <<N - min + 1;
}
for (j = N - 1; j>= 1; j--) {
min = i <j ? i : j;
cout <<N - min + 1;
}
cout <<endl;
}
for (i = N - 1; i>= 1; i--) {
for (j = 1; j <= N; j++) {
min = i <j ? i : j;
cout <<N - min + 1;
}
for (j = N - 1; j>= 1; j--) {
min = i <j ? i : j;
cout <<N - min + 1;
}
cout <<endl;
}
return 0;
}``````

## Java

``````//Java implementation of the approach
class GFG
{
public static void main(String[] args)
{
int N = 4 , i, j, min;
System.out.println( "Value of N: " + N);

for (i = 1 ; i <= N; i++)
{
for (j = 1 ; j <= N; j++)
{
min = i <j ? i : j;
System.out.print(N - min + 1 );
}
for (j = N - 1 ; j>= 1 ; j--)
{
min = i <j ? i : j;
System.out.print(N - min + 1 );
}
System.out.println();
}

for (i = N - 1 ; i>= 1 ; i--)
{
for (j = 1 ; j <= N; j++)
{
min = i <j ? i : j;
System.out.print(N - min + 1 );
}
for (j = N - 1 ; j>= 1 ; j--)
{
min = i <j ? i : j;
System.out.print(N - min + 1 );
}
System.out.println();
}
}
}

//This code is contributed by 29AjayKumar``````

## Python3

``````# Python3 implementation of the approach
N = 4
print ( "Value of N: " , N)

for i in range ( 1 , N + 1 ):
for j in range ( 1 , N + 1 ):
min = i if i <j else j
print (N - min + 1 , end = " " )

for j in range (N - 1 , 0 , - 1 ):
min = i if i <j else j
print (N - min + 1 , end = " " )

print ()

for i in range (N - 1 , 0 , - 1 ):
for j in range ( 1 , N + 1 ):
min = i if i <j else j
print (N - min + 1 , end = " " )

for j in range (N - 1 , 0 , - 1 ):
min = i if i <j else j
print (N - min + 1 , end = " " )

print ()

# This code is contributed by sai prakash``````

## C#

``````//C# implementation of the approach
using System;

class GFG
{
public static void Main(String[] args)
{
int N = 4, i, j, min;
Console.WriteLine( "Value of N: " + N);

for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
min = i <j ? i : j;
Console.Write(N - min + 1);
}
for (j = N - 1; j>= 1; j--)
{
min = i <j ? i : j;
Console.Write(N - min + 1);
}
Console.WriteLine();
}

for (i = N - 1; i>= 1; i--)
{
for (j = 1; j <= N; j++)
{
min = i <j ? i : j;
Console.Write(N - min + 1);
}
for (j = N - 1; j>= 1; j--)
{
min = i <j ? i : j;
Console.Write(N - min + 1);
}
Console.WriteLine();
}
}
}

//This code is contributed by Rajput-Ji``````

``````Value of N: 4
4444444
4333334
4322234
4321234
4322234
4333334
4444444``````