算法设计:打印对称双三角图案

2021年3月11日18:01:21 发表评论 803 次浏览

本文概述

给定一个值n, 我们需要仅使用恒定的额外空间相应地打印以下模式。

例子:

Input : n = 1
Output : x

Input : n = 2
Output :  
 x 
x x 
 x 

Input: n = 5
Output:
    x                                   
     x 
    o x 
     o x 
x o x o x 
 x o 
  x o 
   x 
    x 

Input: n = 6
Output:
     x 
      x 
     o x 
      o x 
     x o x 
x o x x o x 
 x o x 
  x o 
   x o 
    x 
     x

Input : n = 7;
Output :
      x 
       x 
      o x 
       o x 
      x o x 
       x o x 
x o x o x o x 
 x o x 
  x o x 
   x o 
    x o 
     x 
      x 

Input : n = 8;
Output : 
       x 
        x 
       o x 
        o x 
       x o x 
        x o x 
       o x o x 
x o x o o x o x 
 x o x o 
  x o x 
   x o x 
    x o 
     x o 
      x 
       x

推荐:请尝试以下方法{IDE}首先, 在继续解决方案之前。

我们可以将此问题分为三个部分:

1)用奇数n的n-1行或偶数n的n-2行打印上半部分。

2)打印中间行, 奇数n为1行, 偶数n为3行。

3)打印下半部分, 对奇数n用n-1行, 对于偶数n用n-2行。

对于这种复杂的模式, 如果我们可以使用基于1的索引可能会更容易

以及单独的功能来打印以x或o开头的字符。

C ++

// Author:: Satish Srinivas
#include <iostream>
  
using namespace std;
  
// print alternate x o beginning with x
void printx( int n)
{
     for ( int i = 1; i <= n; i++) {
         if (i % 2 != 0)
             cout << "x " ;
         else
             cout << "o " ;
     }
     return ;
}
  
// print alternate x o beginning with o
void printo( int n)
{
     for ( int i = 1; i <= n; i++) {
         if (i % 2 != 0)
             cout << "o " ;
         else
             cout << "x " ;
     }
     return ;
}
  
// print the pattern for n
void printPattern( int n)
{
     // upper half
     // n-1 lines for odd, n-2 lines for even
     int x = n;
  
     if (n % 2 == 0)
         x = x - 1;
  
     // number of spaces to leave in each line
     int p = n - 1;
  
     // number of characters in each line
     int s = 1;
  
     // prints double lines in each iteration
     for ( int i = 1; i <= (x - 1) / 2; i++) {
         for ( int j = 1; j <= p; j++) {
             cout << " " ;
         }
  
         if (i % 2 != 0)
             printx(s);
         else
             printo(s);
  
         cout << endl;
         p++;
  
         for ( int j = 1; j <= p; j++) 
             cout << " " ;        
  
         if (i % 2 != 0)
             printx(s);
         else
             printo(s);
  
         cout << endl;
  
         p--;
         s++;
     }
  
     // extra upper middle for even
     if (n % 2 == 0) {
         for ( int i = 1; i <= p; i++)
             cout << " " ;
  
         if (n % 4 != 0)
             printx(n / 2);
         else
             printo(n / 2);
  
         cout << endl;
     }
  
     // middle line
     if (n % 2 != 0) 
         printx(n);
      
     else {
         if (n % 4 != 0) {
             printx(n / 2);
             printx(n / 2);
         }
         else {
             printx(n / 2);
             printo(n / 2);
         }
     }
  
     cout << endl;
  
     // extra lower middle for even
     if (n % 2 == 0) {
         cout << " " ;
         printx(n / 2);
         cout << endl;
     }
  
     // lower half
     p = 1;
  
     if (n % 2 == 0) {
         x--;
         p = 2;
     }
  
     int q = x / 2;
  
     // one line for each iteration
     for ( int i = 1; i <= x; i++) {
         for ( int j = 1; j <= p; j++)
             cout << " " ;
  
         printx(q);
  
         if (i % 2 == 0)
             q--;
  
         cout << endl;
  
         p++;
     }
  
     cout << endl;
}
  
// Driver code
int main()
{
     int n = 7;
     printPattern(n);
     n = 8;
     printPattern(n);
     return 0;
}

Java

// java program to Print symmetric 
// double triangle pattern
class GFG
{
          
     // print alternate x o beginning with x
     static void printx( int n)
     {
         for ( int i = 1 ; i <= n; i++) {
             if (i % 2 != 0 )
                 System.out.print( "x " );
             else
                 System.out.print( "o " );
         }
         return ;
     }
      
     // print alternate x o beginning with o
     static void printo( int n)
     {
         for ( int i = 1 ; i <= n; i++) {
             if (i % 2 != 0 )
                 System.out.print( "o " );
             else
                 System.out.print( "x " );
         }
         return ;
     }
      
     // print the pattern for n
     static void printPattern( int n)
     {
         // upper half n-1 lines for
         // odd, n-2 lines for even
         int x = n;
      
         if (n % 2 == 0 )
             x = x - 1 ;
      
         // number of spaces to leave in each line
         int p = n - 1 ;
      
         // number of characters in each line
         int s = 1 ;
      
         // prints double lines in each iteration
         for ( int i = 1 ; i <= (x - 1 ) / 2 ; i++) {
             for ( int j = 1 ; j <= p; j++) {
                 System.out.print( " " );
             }
      
             if (i % 2 != 0 )
                 printx(s);
             else
                 printo(s);
      
             System.out.println();
             p++;
      
             for ( int j = 1 ; j <= p; j++) 
                 System.out.print( " " );     
      
             if (i % 2 != 0 )
                 printx(s);
             else
                 printo(s);
      
             System.out.println();
      
             p--;
             s++;
         }
      
         // extra upper middle for even
         if (n % 2 == 0 ) {
             for ( int i = 1 ; i <= p; i++)
                 System.out.print( " " );
      
             if (n % 4 != 0 )
                 printx(n / 2 );
             else
                 printo(n / 2 );
      
             System.out.println();
         }
      
         // middle line
         if (n % 2 != 0 ) 
             printx(n);
          
         else {
             if (n % 4 != 0 ) {
                 printx(n / 2 );
                 printx(n / 2 );
             }
             else {
                 printx(n / 2 );
                 printo(n / 2 );
             }
         }
      
         System.out.println();
      
         // extra lower middle for even
         if (n % 2 == 0 ) {
             System.out.print( " " );
             printx(n / 2 );
             System.out.println();
         }
      
         // lower half
         p = 1 ;
      
         if (n % 2 == 0 ) {
             x--;
             p = 2 ;
         }
      
         int q = x / 2 ;
      
         // one line for each iteration
         for ( int i = 1 ; i <= x; i++) {
             for ( int j = 1 ; j <= p; j++)
                 System.out.print( " " );
      
             printx(q);
      
             if (i % 2 == 0 )
                 q--;
      
             System.out.println();
      
             p++;
         }
      
         System.out.println();
     } 
      
     // Driver code
     public static void main (String[] args)
     {
         int n = 7 ;
         printPattern(n);
          
         n = 8 ;
         printPattern(n);
     }
}
  
  
// This code is contributed by Anant Agarwal.

Python3

# Python3 program to Print symmetric 
# double triangle pattern
  
# Print alternate x o beginning with x
def printx(n):
  
     for i in range ( 1 , n + 1 ):
         if (i % 2 ! = 0 ):
             print ( "x " , end = "")
         else :
             print ( "o " , end = "")
  
     return
  
# Print alternate x o beginning with o
def printo(n):
  
     for i in range ( 1 , n + 1 ):
         if (i % 2 ! = 0 ):
             print ( "o " , end = "")
         else :
             print ( "x " , end = "")
  
     return
  
# Print the pattern for n
def printPattern(n):
  
     # upper half
     # n-1 lines for odd, # n-2 lines for even
     x = n
  
     if (n % 2 = = 0 ):
         x = x - 1
  
     # number of spaces to leave
     # in each line
     p = n - 1
  
     # number of characters in each line
     s = 1
  
     # prints double lines in each iteration
     for i in range ( 1 , (x - 1 ) / / 2 + 1 ):
         for j in range ( 1 , p + 1 ):
             print ( " " , end = "")
  
         if (i % 2 ! = 0 ):
             printx(s)
         else :
             printo(s)
  
         print ()
         p + = 1
  
         for j in range ( 1 , p + 1 ):
             print ( " " , end = "")
  
         if (i % 2 ! = 0 ):
             printx(s)
         else :
             printo(s)
  
         print ()
  
         p - = 1
         s + = 1
  
     # extra upper middle for even
     if (n % 2 = = 0 ):
         for i in range ( 1 , p + 1 ):
             print ( " " , end = "")
  
         if (n % 4 ! = 0 ):
             printx(n / / 2 )
         else :
             printo(n / / 2 )
  
         print ()
  
     # middle line
     if (n % 2 ! = 0 ):
         printx(n)
     else :
         if (n % 4 ! = 0 ):
             printx(n / / 2 )
             printx(n / / 2 )
         else :
             printx(n / / 2 )
             printo(n / / 2 )
  
     print ()
  
     # extra lower middle for even
     if (n % 2 = = 0 ):
         print ( " " , end = "")
         printx(n / / 2 )
         print ()
  
     # lower half
     p = 1
  
     if (n % 2 = = 0 ):
         x - = 1
         p = 2
  
     q = x / / 2
  
     # one line for each iteration
     for i in range ( 1 , x + 1 ):
         for j in range ( 1 , p + 1 ):
             print ( " " , end = "")
  
         printx(q)
  
         if (i % 2 = = 0 ):
             q - = 1
  
         print ()
  
         p + = 1
  
     print ()
  
# Driver code
n = 7
printPattern(n)
n = 8
printPattern(n)
  
# This code is contributed by mohit kumar

C#

// C# program to Print symmetric 
// double triangle pattern
using System;
class GFG
{
          
     // print alternate x o beginning with x
     static void printx(int n)
     {
         for (int i = 1; i <= n; i++) {
             if (i % 2 != 0)
                 Console.Write("x ");
             else
                 Console.Write("o ");
         }
         return;
     }
      
     // print alternate x o beginning with o
     static void printo(int n)
     {
         for (int i = 1; i <= n; i++) {
             if (i % 2 != 0)
                 Console.Write("o ");
             else
                 Console.Write("x ");
         }
         return;
     }
      
     // print the pattern for n
     static void printPattern(int n)
     {
         // upper half n-1 lines for
         // odd, n-2 lines for even
         int x = n;
      
         if (n % 2 == 0)
             x = x - 1;
      
         // number of spaces to leave in each line
         int p = n - 1;
      
         // number of characters in each line
         int s = 1;
      
         // prints double lines in each iteration
         for (int i = 1; i <= (x - 1) / 2; i++) {
             for (int j = 1; j <= p; j++) {
                 Console.Write(" ");
             }
      
             if (i % 2 != 0)
                 printx(s);
             else
                 printo(s);
      
             Console.WriteLine();
             p++;
      
             for (int j = 1; j <= p; j++) 
                 Console.Write(" ");     
      
             if (i % 2 != 0)
                 printx(s);
             else
                 printo(s);
      
           Console.WriteLine();
      
             p--;
             s++;
         }
      
         // extra upper middle for even
         if (n % 2 == 0) {
             for (int i = 1; i <= p; i++)
                 Console.Write(" ");
      
             if (n % 4 != 0)
                 printx(n / 2);
             else
                 printo(n / 2);
      
             Console.WriteLine();
         }
      
         // middle line
         if (n % 2 != 0) 
             printx(n);
          
         else {
             if (n % 4 != 0) {
                 printx(n / 2);
                 printx(n / 2);
             }
             else {
                 printx(n / 2);
                 printo(n / 2);
             }
         }
      
           Console.WriteLine();
      
         // extra lower middle for even
         if (n % 2 == 0) {
             Console.Write(" ");
             printx(n / 2);
             Console.WriteLine();
         }
      
         // lower half
         p = 1;
      
         if (n % 2 == 0) {
             x--;
             p = 2;
         }
      
         int q = x / 2;
      
         // one line for each iteration
         for (int i = 1; i <= x; i++) {
             for (int j = 1; j <= p; j++)
                 Console.Write(" ");
      
             printx(q);
      
             if (i % 2 == 0)
                 q--;
      
             Console.WriteLine();
      
             p++;
         }
      
         Console.WriteLine();
     } 
      
     // Driver code
     public static void Main ()
     {
         int n = 7;
         printPattern(n);
          
         n = 8;
         printPattern(n);
     }
}
  
  
// This code is contributed by vt_m.

的PHP

<?php
// PHP program to Print symmetric 
// double triangle pattern
  
// print alternate x o 
// beginning with x
function printx( $n )
{
     for ( $i = 1; $i <= $n ; $i ++) 
     {
         if ( $i % 2 != 0)
             echo "x " ;
         else
             echo "o " ;
     }
     return ;
}
  
// print alternate x o 
// beginning with o
function printo( $n )
{
     for ( $i = 1; $i <= $n ; $i ++) 
     {
         if ( $i % 2 != 0)
             echo "o " ;
         else
             echo "x " ;
     }
}
  
// print the pattern for n
function printPattern( $n )
{
     // upper half
     // n-1 lines for odd, // n-2 lines for even
     $x = $n ;
  
     if ( $n % 2 == 0)
         $x = $x - 1;
  
     // number of spaces to 
     // leave in each line
     $p = $n - 1;
  
     // number of characters 
     // in each line
     $s = 1;
  
     // prints double lines
     // in each iteration
     for ( $i = 1; $i <= ( $x - 1) / 2; $i ++)
     {
         for ( $j = 1; $j <= $p ; $j ++) 
         {
             echo " " ;
         }
  
         if ( $i % 2 != 0)
             printx( $s );
         else
             printo( $s );
  
         echo "\n" ;
         $p ++;
  
         for ( $j = 1; $j <= $p ; $j ++) 
             echo " " ; 
  
         if ( $i % 2 != 0)
             printx( $s );
         else
             printo( $s );
  
         echo "\n" ;
  
         $p --;
         $s ++;
     }
  
     // extra upper middle
     // for even
     if ( $n % 2 == 0) {
         for ( $i = 1; $i <= $p ; $i ++)
             echo " " ;
  
         if ( $n % 4 != 0)
             printx( $n / 2);
         else
             printo( $n / 2);
  
         echo "\n" ;
     }
  
     // middle line
     if ( $n % 2 != 0) 
         printx( $n );
      
     else {
         if ( $n % 4 != 0) 
         {
             printx( $n / 2);
             printx( $n / 2);
         }
         else {
             printx( $n / 2);
             printo( $n / 2);
         }
     }
  
     echo "\n" ;
  
     // extra lower middle for even
     if ( $n % 2 == 0) {
         echo " " ;
         printx( $n / 2);
         echo "\n" ;
     }
  
     // lower half
     $p = 1;
  
     if ( $n % 2 == 0) 
     {
         $x --;
         $p = 2;
     }
  
     $q = floor ( $x / 2);
  
     // one line for each iteration
     for ( $i = 1; $i <= $x ; $i ++)
     {
         for ( $j = 1; $j <= $p ; $j ++)
             echo " " ;
  
         printx( $q );
  
         if ( $i % 2 == 0)
             $q --;
  
         echo "\n" ;
  
         $p ++;
     }
  
     echo "\n" ;
}
  
     // Driver code
     $n = 7;
     printPattern( $n );
     $n = 8;
     printPattern( $n );
      
// This code is contributed by mits
?>

输出如下:

x 
       x 
      o x 
       o x 
      x o x 
       x o x 
x o x o x o x 
 x o x 
  x o x 
   x o 
    x o 
     x 
      x 

       x 
        x 
       o x 
        o x 
       x o x 
        x o x 
       o x o x 
x o x o o x o x 
 x o x o 
  x o x 
   x o x 
    x o 
     x o 
      x 
       x

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: