📜  程序打印反向字符桥图案

📅  最后修改于: 2021-05-31 18:58:13             🧑  作者: Mango

对于给定的值N(表示从A开始的宪章数量),打印反向字符桥模式。


例子 :

Input : n = 5
Output :

 ABCDEDCBA
 ABCD DCBA
 ABC   CBA
 AB     BA
 A       A


Input : n = 8
Output :

ABCDEFGHGFEDCBA
ABCDEFG GFEDCBA
ABCDEF   FEDCBA
ABCDE     EDCBA
ABCD       DCBA
ABC         CBA
AB           BA
A             A
  • 对于给定的值N,从A开始反映参与模式的字符数。对于N = 5,参与的字符将为ABCDE。
  • 通过使用嵌套的for循环,我们将计算逻辑。其中“ i”的外循环范围是0到N,而“ j”的内循环范围是65(开始)到64 + 2 * N。
  • 在此情况下,我们将检查图案设计所需的条件。对于所有小于(((64 + n)+ i)的j值,它将打印(char)((64 + n)-(j%(64 + n)))和所有j的值<=((64 + n)-i)它会打印(char)j。
C++
// CPP program to print reverse character bridge pattern
#include 
using namespace std;
  
// Function to print pattern
void ReverseCharBridge(int n)
{
    for (int i = 0; i < n; i++) 
    {
        for (int j = 'A'; j < 'A' + (2 * n) - 1; j++) 
        {
            if (j >= ('A' + n - 1) + i)
                cout << (char)(('A' + n - 1) - 
                               (j % ('A' + n - 1)));
            else if (j <= ('A' + n - 1) - i)
                cout << (char)j;
            else
                cout << " ";
        }
        cout << endl;
    }
}
  
// Driver Code
int main()
{
    int n = 6;
    ReverseCharBridge(n);
    return 0;
}


Java
// Java program to print reverse
// character bridge pattern
import java.io.*;
  
class GFG {
      
    // Function to print pattern
    static void ReverseCharBridge(int n)
    {
        for (int i = 0; i < n; i++) 
        {
            for (int j = 'A'; j < 'A' + (2 * n) - 1; j++) 
            {
              if (j >= ('A' + n - 1) + i)
                System.out.print((char)(('A' + n - 1) - 
                                 (j % ('A' + n - 1))));
                else if (j <= ('A' + n - 1) - i)
                    System.out.print((char)j);
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int n = 6;
        ReverseCharBridge(n);
    }
}
  
/*This code is contributed by Nikita Tiwari.*/


Python3
# Python3 code to print reverse 
# character bridge pattern
  
# Function to print pattern
def ReverseCharBridge( n ):
    for i in range( n ):
        for j in range( ord('A'), ord('A') + 
                              (2 * n) - 1):
            if j >= (ord( 'A' ) + n - 1) + i:
                print(chr((ord('A') + n - 1) - 
                  (j % (ord('A') + n - 1))), end = '')
              
            elif j <= (ord('A') + n - 1) - i:
                print(chr(j), end = '')
            else:
                print(end = " ")
        print("\n", end = '')
          
# Driver Code
n = 6
ReverseCharBridge(n)
  
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program to print reverse
// character bridge pattern
using System;
  
class GFG {
  
    // Function to print pattern
    static void ReverseCharBridge(int n)
    {
        for (int i = 0; i < n; i++) 
        {
            for (int j = 'A'; j < 'A' + (2 * n) - 1; j++)
            {
                if (j >= ('A' + n - 1) + i)
                    Console.Write((char)(('A' + n - 1)
                    - (j % ('A' + n - 1))));
                  
                else if (j <= ('A' + n - 1) - i)
                    Console.Write((char)j);
                  
                else
                    Console.Write(" ");
            }
            Console.WriteLine();
        }
    }
  
    // Driver Code
    public static void Main()
    {
        int n = 6;
        ReverseCharBridge(n);
    }
}
  
// This code is contributed by vt_m.


PHP
= (65 + $n - 1) + $i)
                echo chr((65 + $n - 1) - 
                    ($j % (65 + $n - 1)));
                      
            else if ($j <= (65 + $n - 1) - $i)
                echo chr($j);
            else
                echo " ";
        }
        echo "\n";
    }
}
  
// Driver Code
$n = 6;
ReverseCharBridge($n);
  
// This code is contributed by mits 
?>


输出 :

ABCDEFEDCBA
ABCDE EDCBA
ABCD   DCBA
ABC     CBA
AB       BA
A         A
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”