📜  程序打印锯齿形图案

📅  最后修改于: 2021-04-22 02:30:26             🧑  作者: Mango

给定数字N表示行数。任务是打印N行的锯齿形图案,如以下示例所示。
例子

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

Input : N = 5
Output : 1 
         3*2 
         4*5*6 
         10*9*8*7 
         11*12*13*14*15

方法

  1. 使用for循环打印行数。
  2. 将两个变量var和var1分别用于奇数行和偶数行。
  3. 当行号为奇数时,请计算行的起点,然后同时打印并递增变量。
  4. 当行数为偶数时,计算相应的起点并同时打印并减小变量。

下面是上述方法的实现:

C++
// CPP program to print the given
// zigzag pattern
 
#include
using namespace std;
 
// Function to print the zigzag pattern
void printPattern(int n)
{  
    int var1, var = 1;
     
    for(int i = 1; i <= n; i++)
    {  
        // for odd rows
        if(i%2!=0)
        {  
            // calculate starting value
            var = var + i - 1;
            for(int j=1; j<=i; j++)
            {
                if(j==1)
                {
                    cout<


Java
// Java program to print the given
// zigzag pattern
class GFG
{
// Function to print the
// zigzag pattern
static void printPattern(int n)
{
    int var1, var = 1;
     
    for(int i = 1; i <= n; i++)
    {
        // for odd rows
        if(i % 2 != 0)
        {
            // calculate starting value
            var = var + i - 1;
            for(int j = 1; j <= i; j++)
            {
                if(j == 1)
                {
                    System.out.print(var);
                }
                else
                System.out.print("*" + var);
                     
            var++;
            }
        }
        else // for even rows
        {    
            var1 = var + i -1; // calculate starting value
            for(int j = 1; j <= i; j++)
            {
                if(j == 1)
                {
                    // print without star
                    System.out.print(var1);
                }
                else
                {
                    // print with star
                    System.out.print("*" + var1);
                }
                var1--;
            }
        }
        System.out.print("\n");
    }
     
}
 
// Driver code
public static void main(String [] arg)
{
    int n = 5;
     
    printPattern(n);
}
}
 
// This code is contributed by Smitha


Python3
# Python3 program to print the given
# zigzag pattern
 
# Function to print the zigzag pattern
def printPattern(n):
     
    var = 0
    var = 1
     
    for i in range(1, n + 1):
 
        # for odd rows
        if(i % 2 != 0):
             
            # calculate starting value
            var = var + i - 1
            for j in range(1, i + 1):
             
                if(j == 1):
                 
                    print(var, end = "")
                else:
                    print("*", end = "")
                    print(var, end = "")
                     
                var += 1
             
         
        else: # for even rows
                 
            var1 = var + i -1 # calculate starting value
            for j in range(1, i + 1):
             
                if(j == 1):
                 
                    # prwithout star
                    print(var1, end = "")
                 
                else:
                 
                    # prwith star
                    print("*", end = "")
                    print(var1, end = "")
                 
                var1 -= 1
             
        print()
     
# Driver code
n = 5
 
printPattern(n)
 
# This code is contributed by Mohit kumar


C#
// C# program to print the given
// zigzag pattern
using System;
class GFG
{
// Function to print the
// zigzag pattern
static void printPattern(int n)
{
    int var1, var = 1;
     
    for(int i = 1; i <= n; i++)
    {
        // for odd rows
        if(i % 2 != 0)
        {
            // calculate starting value
            var = var + i - 1;
            for(int j = 1; j <= i; j++)
            {
                if(j == 1)
                {
                    Console.Write(var);
                }
                else
                    Console.Write("*" + var);
                     
            var++;
            }
        }
        else // for even rows
        {    
            var1 = var + i -1; // calculate starting value
            for(int j = 1; j <= i; j++)
            {
                if(j == 1)
                {
                    // print without star
                    Console.Write(var1);
                }
                else
                {
                    // print with star
                    Console.Write("*" + var1);
                }
                var1--;
            }
        }
        Console.Write("\n");
    }
     
}
 
// Driver code
public static void Main()
{
    int n = 5;
     
    printPattern(n);
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15