本题要求实现一个函数,输出n行空心的数字金字塔
#include<iostream>
using namespace std;
void fact(int n)
{
int arr[10][10] = { 0 };
for (int i = 0; i < n; i++)
{
//输出空格
for (int k = n; k > i; --k)
{
cout << " ";
}
//输出数字
if (i==n-1||i==0)//第一行数字和最后一行数字
{
for (int j = 0; j < 2 * i + 1; j++)
{
arr[i][j] = i + 1;
cout << arr[i][j];
}
}
else //中间的数字和空格
{
for (int q = 0; q<2 * i + 1; q++)
{
if (q==0||q== 2 * i)
{
arr[i][q] = i + 1;
cout << arr[i][q];
}
else
{
cout << " ";
}
}
}
cout << endl;
}
}
int main()
{
int n;
cin >> n;
fact(n);
return 0;
}