📌  相关文章
📜  程序使用1到n的数字模式打印’N’字母

📅  最后修改于: 2021-04-26 06:32:38             🧑  作者: Mango

给定整数N ,任务是打印字母N模式,如下所示:

1           1
2 2         2
3   3       3
*    *      *
*      *    *
*        *  *
N           N

例子:

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

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

方法:除第一行和最后一行外,每隔一行将遵循以下内容:

  • 将第一个值打印为index +1 ,其中index是该行的索引。
  • 然后将空白打印2 *索引倍。
  • 再次将值index +1打印为当前行的对角线元素。
  • 然后打印其余2 *(N – index – 1)个空格,后跟结束元素,即index + 1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the desired Alphabet N Pattern
void Alphabet_N_Pattern(int N)
{
 
    int index, side_index, size;
 
    // Declaring the values of Right, Left and Diagonal values
    int Right = 1, Left = 1, Diagonal = 2;
 
    // Main Loop for the rows
    for (index = 0; index < N; index++) {
 
        // For the left Values
        cout << Left++;
 
        // Spaces for the diagonals
        for (side_index = 0; side_index < 2 * (index); side_index++)
            cout << " ";
 
        // Condition for the diagonals
        if (index != 0 && index != N - 1)
            cout << Diagonal++;
        else
            cout << " ";
 
        // Spaces for the Right Values
        for (side_index = 0; side_index < 2 * (N - index - 1); side_index++)
            cout << " ";
 
        // For the right values
        cout << Right++;
 
        cout << endl;
    }
}
 
// Driver Code
int main(int argc, char** argv)
{
    // Size of the Pattern
    int Size = 6;
 
    // Calling the function to print the desired Pattern
    Alphabet_N_Pattern(Size);
}


C
// C implementation of the approach
#include 
 
// Function to print the desired Alphabet N Pattern
void Alphabet_N_Pattern(int N)
{
 
    int index, side_index, size;
 
    // Declaring the values of Right, Left and Diagonal values
    int Right = 1, Left = 1, Diagonal = 2;
 
    // Main Loop for the rows
    for (index = 0; index < N; index++) {
 
        // For the left Values
        printf("%d", Left++);
 
        // Spaces for the diagonals
        for (side_index = 0; side_index < 2 * (index); side_index++)
            printf(" ");
 
        // Condition for the diagonals
        if (index != 0 && index != N - 1)
            printf("%d", Diagonal++);
        else
            printf(" ");
 
        // Spaces for the Right Values
        for (side_index = 0; side_index < 2 * (N - index - 1); side_index++)
            printf(" ");
 
        // For the right values
        printf("%d", Right++);
 
        printf("\n");
    }
}
 
// Driver Code
int main(int argc, char** argv)
{
    // Size of the Pattern
    int Size = 6;
 
    // Calling the function to print the desired Pattern
    Alphabet_N_Pattern(Size);
}


Java
// Java implementation of the approach
import java.util.*;
 
 
class solution
{
 
// Function to print the desired Alphabet N Pattern
static void Alphabet_N_Pattern(int N)
{
 
    int index, side_index, size;
 
    // Declaring the values of Right, Left and Diagonal values
    int Right = 1, Left = 1, Diagonal = 2;
 
    // Main Loop for the rows
    for (index = 0; index < N; index++) {
 
        // For the left Values
        System.out.print(Left++);
 
        // Spaces for the diagonals
        for (side_index = 0; side_index < 2 * (index); side_index++)
            System.out.print(" ");
 
        // Condition for the diagonals
        if (index != 0 && index != N - 1)
            System.out.print(Diagonal++);
        else
            System.out.print(" ");
 
        // Spaces for the Right Values
        for (side_index = 0; side_index < 2 * (N - index - 1); side_index++)
            System.out.print(" ");
 
        // For the right values
        System.out.print(Right++);
 
        System.out.println();
    }
}
 
// Driver Code
public static void main(String args[])
{
    // Size of the Pattern
    int Size = 6;
 
    // Calling the function to print the desired Pattern
    Alphabet_N_Pattern(Size);
}
 
}
 
// This code is contributed by
// Surendra_Gagwar


Python3
# Python 3 implementation of the approach
 
# Function to print the desired
# Alphabet N Pattern
def Alphabet_N_Pattern(N):
     
    # Declaring the values of Right, Left
    # and Diagonal values
    Right = 1
    Left = 1
    Diagonal = 2
 
    # Main Loop for the rows
    for index in range(N):
         
        # For the left Values
        print(Left, end = "")
        Left += 1
 
        # Spaces for the diagonals
        for side_index in range(0, 2 * (index), 1):
            print(" ", end = "")
 
        # Condition for the diagonals
        if (index != 0 and index != N - 1):
            print(Diagonal, end = "")
            Diagonal += 1
        else:
            print(" ", end = "")
 
        # Spaces for the Right Values
        for side_index in range(0, 2 * (N - index - 1), 1):
            print(" ", end = "")
 
        # For the right values
        print(Right, end = "")
        Right += 1
         
        print("\n", end = "")
     
# Driver Code
if __name__ == '__main__':
     
    # Size of the Pattern
    Size = 6
 
    # Calling the function to print
    # the desired Pattern
    Alphabet_N_Pattern(Size)
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to print the desired Alphabet N Pattern
public static void Alphabet_N_Pattern(int N)
{
 
    int index, side_index;
     
    // Declaring the values of Right,
    // Left and Diagonal values
    int Right = 1, Left = 1, Diagonal = 2;
     
    // Main Loop for the rows
    for (index = 0; index < N; index++)
    {
 
        // For the left Values
        Console.Write(Left++);
 
        // Spaces for the diagonals
        for (side_index = 0;
             side_index < 2 * (index); side_index++)
            Console.Write(" ");
 
        // Condition for the diagonals
        if (index != 0 && index != N - 1)
            Console.Write(Diagonal++);
        else
            Console.Write(" ");
         
        // Spaces for the Right Values
        for (side_index = 0;
             side_index < 2 * (N - index - 1);
             side_index++)
            Console.Write(" ");
 
        // For the right values
        Console.Write(Right++);
 
        Console.Write("\n");
    }
}
 
// Driver Code
static void Main()
{
    // Size of the Pattern
    int Size = 6;
 
    // Calling the function to print
    // the desired Pattern
    Alphabet_N_Pattern(Size);
}
}
 
// This code is contributed by DrRoot_


PHP


Javascript


输出:
1           1
2  2        2
3    3      3
4      4    4
5        5  5
6           6