在 C++ 中列印金字塔
本文使用 C++ 程式設計程式碼生成了“類金字塔結構”的輸出。其中,金字塔的高度和間隔由遍歷雙重 for 迴圈構造來決定,如下所示;
示例
#include <iostream>
using namespace std;
int main() {
int space, rows=6;
for(int i = 1, k = 0; i <= rows; ++i, k = 0){
for(space = 1; space <= rows-i; ++space){
cout <<" ";
}
while(k != 2*i-1){
cout << "* ";
++k;
}
cout << endl;
}
return 0;
}輸出
在編譯完上述程式碼後,將列印類似如下內容的金字塔。
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP