在 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;
}输出
在编译完上述代码后,半金字塔将被打印出来,如下所示。
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP