在 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 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告