#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; }