📜  程序打印修改后的二进制三角形图案

📅  最后修改于: 2021-04-24 19:21:43             🧑  作者: Mango

给定整数N ,任务是打印修改后的二叉树图案。

例子:

方法:从以上示例可以看出,对于每行N:

  • 第一个和第N个元素是1
  • 元素2至(N-1)为0

因此,可以开发一种算法来打印以下图案:

  • 使用循环变量i(从1到N)执行循环,循环变量i表示三角形图案的行号。
  • 对于每行i,使用循环变量j表示从1到i的循环,该变量表示每行中的数字。
  • 在j的每次迭代中,检查j是1还是i。如果任何一个为真,则打印1。
  • 如果j都不满足,则打印0
  • 每次迭代后将j的值加1
  • 当j循环成功完成时,我们已经打印了一行模式。因此,通过打印下一行将输出更改为下一行。
  • 增大i的值并重复整个过程,直到成功打印N行。

下面是上述方法的实现:

C++
// C++ implementation to print the
// modified binary triangle pattern
 
#include 
using namespace std;
 
// Function to print the modified
// binary pattern
void modifiedBinaryPattern(int n)
{
 
    // Loop to traverse the rows
    for (int i = 1; i <= n; i++) {
 
        // Loop to traverse the
        // numbers in each row
        for (int j = 1; j <= i; j++) {
 
            // Check if j is 1 or i
            // In either case print 1
            if (j == 1 || j == i)
                cout << 1;
 
            // Else print 0
            else
                cout << 0;
        }
 
        // Change the cursor to next
        // line after each row
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    int n = 7;
 
    // Function Call
    modifiedBinaryPattern(n);
}


Java
// Java implementation to print the
// modified binary triangle pattern
import java.io.*;
 
class GFG{
 
// Function to print the modified
// binary pattern
static void modifiedBinaryPattern(int n)
{
 
    // Loop to traverse the rows
    for(int i = 1; i <= n; i++)
    {
        
       // Loop to traverse the
       // numbers in each row
       for(int j = 1; j <= i; j++)
       {
            
           // Check if j is 1 or i
           // In either case print 1
           if (j == 1 || j == i)
               System.out.print(1);
            
           // Else print 0
           else
               System.out.print(0);
        }
         
        // Change the cursor to next
        // line after each row
        System.out.println();
    }
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 7;
 
    // Function call
    modifiedBinaryPattern(n);
}
}
 
// This code is contributed by shubhamsingh10


Python 3
# python3 implementation to print the
# modified binary triangle pattern
 
# Function to print the modified
# binary pattern
def modifiedBinaryPattern(n):
     
    # Loop to traverse the rows
    for i in range(1, n + 1, 1):
 
        # Loop to traverse the
        # numbers in each row
        for j in range(1, i + 1, 1):
             
            # Check if j is 1 or i
            # In either case print 1
            if (j == 1 or j == i):
                print(1, end = "")
 
            # Else print 0
            else:
                print(0, end = "")
 
        # Change the cursor to next
        # line after each row
        print('\n', end = "")
 
# Driver Code
if __name__ == '__main__':
     
    n = 7
     
    # Function Call
    modifiedBinaryPattern(n)
 
# This code is contributed by Samarth


C#
// C# implementation to print the
// modified binary triangle pattern
using System;
class GFG{
 
// Function to print the modified
// binary pattern
static void modifiedBinaryPattern(int n)
{
 
    // Loop to traverse the rows
    for(int i = 1; i <= n; i++)
    {
         
        // Loop to traverse the
        // numbers in each row
        for(int j = 1; j <= i; j++)
        {
             
            // Check if j is 1 or i
            // In either case print 1
            if (j == 1 || j == i)
                Console.Write(1);
                 
            // Else print 0
            else
                Console.Write(0);
        }
         
        // Change the cursor to next
        // line after each row
        Console.WriteLine();
    }
}
 
// Driver Code
public static void Main()
{
    int n = 7;
 
    // Function call
    modifiedBinaryPattern(n);
}
}
 
// This code is contributed by Nidhi_Biet


Javascript


输出:
1
11
101
1001
10001
100001
1000001