在 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