C++ 用函数实现金字塔打印,打印的层数或符号由参数指定
作者:互联网
#include <iostream> #include <windows.h> #include <string> using namespace std; void test(int n,char ch='$') { //可以在此指定默认参数 for (int i = 1; i <= n; i++) { for (int y = 0; y < n - i; y++) { cout << " "; } for (int x = 1; x <= 2 * i - 1; x++) { cout << ch; } cout << endl; } } int main() { int i; char ch; cout << "请输入金字塔的层数:"; cin >> i; cout << "请输入金字塔的符号:"; cin >> ch; test(i,ch); //也可在此输入指定的符号 system("pause"); return 0; }
标签:ch,int,打印,指定,C++,层数,test,include 来源: https://www.cnblogs.com/smartlearn/p/16632824.html