JavaScript 打印 V 和倒 V 图案的程序(Program to print V and inverted-V pattern)

 倒 V 型模式:给定 n 的值,打印倒 V 型模式。
示例: 

输入:n = 5

输出 :

    E
   D D
  C   C
 B     B
A       A

输入:n = 7

输出 :

      G
     F F
    E   E
   D     D
  C       C
 B         B
A           A

下面是打印上述图案的程序:

// JavaScript Implementation to  
      // print the pattern 
  
      // Function definition 
      function pattern(n) { 
        var i, 
          j, 
          k = 0; 
        for (i = n - 1; i >= 0; i--) { 
          // outer gap loop 
          for (j = n - 1; j > k; j--) { 
            document.write("  "); 
          } 
  
          // 65 is ASCII of 'A' 
          document.write(String.fromCharCode(i + 65)); 
  
          // inner gap loop 
          for (j = 1; j < k * 2; j++)  
          document.write("  "); 
  
          if (i < n - 1)  
          document.write(String.fromCharCode(i + 65)); 
          document.write("<br>"); 
          k++; 
        } 
      } 
  
      // Driver code 
        
      // taking size from the user 
      var n = 5; 
        
      // function calling 
      pattern(n);  

 输出:

    E
   D D
  C   C
 B     B
A       A

时间复杂度: O(n 2 ),其中 n 表示给定的输入。

辅助空间: O(1),不需要额外的空间,因此为常数。

V 模式:给定 n 的值,打印 V 模式。

示例: 

输入:n = 5

输出:

E       E
 D     D
  C   C
   B B
    A

输入:n = 7

输出:

G           G
 F         F
  E       E
   D     D
    C   C
     B B
      A

下面是打印上述图案的程序:

// JavaScript Implementation to print the pattern 
      
    // Function definition 
    function pattern(n) 
    { 
        let i, j; 
        for (i = n - 1; i >= 0; i--) 
        { 
            // outer gap loop 
            for (j = n - 1; j > i; j--) 
            { 
                document.write("  "); 
            } 
   
            // 65 is ASCII of 'A' 
            document.write(String.fromCharCode(i + 65)); 
   
            // inner gap loop 
            for (j = 1; j < (i * 2); j++) 
                document.write("  "); 
   
            if (i >= 1) 
                document.write(String.fromCharCode(i + 65)); 
            document.write("<br>"); 
        } 
    } 
      
    // Driver code 
      
    // taking size from the user 
    let n = 5; 
      
    // function calling 
    pattern(n); 
  
// This code is contributed by unknown2108 

 输出: 

E       E
 D     D
  C   C
   B B
    A

时间复杂度: O(n 2 ),其中 n 表示给定的输入。

辅助空间: O(1),不需要额外的空间,因此为常数。

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值