解题思路:
根据题意,先初始化第一个数,这儿定义一个二维数组a[101][101],使之a[0][0]=1;
再者计算a[0][n](1....n)的值,a[i + 1][0] = a[i][0] + i + 1;
最后两层循环赋值 a[i][j] = a[i][j - 1] + i + j - 1 + 2;
最后减列输出
注意事项:
for (int j = 0; j < n-i; j++)
参考代码:
#include<iostream>
using namespace std;
int main()
{
int a[101][101],n;
a[0][0] = 1;
cin >> n;
for (int i = 0; i < n; i++)
{
a[i + 1][0] = a[i][0] + i + 1;
for (int j = 1; j <=n - 1; j++)
{
a[i][j] = a[i][j - 1] + i + j - 1 + 2;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-i; j++)
{
cout << a[i][j]<<" ";
}
cout << endl;
}
return 0;
}
0.0分
0 人评分