在 C++ 中输出不同模式的 Bash


本文旨在使用 C++ 编程语言输出半金字塔模式 bash。在查看要打印的规定模式时,正在编制以下算法来实现我们的目标;

算法

Step-1 Set the length of the Bash (Height)
Step-2 Outer loop to handle the number of rows
Step-3 Inner loop to handle columns
Step-4 Print the pattern with the character (@)
Step-5 Set the pointer to a new line after each row (outside the inner loop)
Step-6 Repeat the loop till the Bash Height

示例

因此,最终通过遵循上述算法雕刻出以下 C++ 源代码,如下所示;

 实时演示

#include <iostream>
using namespace std;
void PrintBash(int n){
   // outer loop to handle number of rows
   for (int i=0; i<n; i++){
      // inner loop to handle number of columns
      for(int j=0; j<=i; j++ ){
         // printing character
         cout << "@ ";
      }
      // ending line after each row
      cout << endl;
   }
}
int main(){
   int Len = 6;
   PrintBash(Len);
   return 0;
}

输出

在编译完上述代码后,半金字塔将被打印出来,如下所示。

@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
@ @ @ @ @ @

更新于:2020 年 1 月 16 日

298 次浏览

开启你的 职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.