本文旨在使用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){
//外循环处理行数
for (int i=0; i<n; i++){
//内部循环以处理列数
for(int j=0; j<=i; j++ ){
//打印字符
cout << "@ ";
}
//每行后的结束行
cout << endl;
}
}
int main(){
int Len = 6;
PrintBash(Len);
return 0;
}输出结果
编译完上面的代码后,将按如下所示打印半金字塔。
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @