文件名:[作业]作者:〈漆黑〉描述:〈 程序设计:根据用户输入的数值,打印杨辉三角形。 〉创建时间:2019.5.22
#include <iostream>
#include <iomanip>
using namespace std
;
int main(void)
{
int length
;
cout
<< "请输入杨辉三角的大小:";
while (!(cin
>> length
)) {
cout
<< "输入错误,请重新输入:";
cin
.clear();
cin
.ignore(1024, '\n');
}
int** nums
= new
int* [length
];
for (int i
= 0; i
< length
; i
++)
{
nums
[i
] = new
int[length
];
}
int i
, j
;
for (i
= 0; i
< length
; i
++)
{
nums
[i
][0] = 1;
nums
[i
][i
] = 1;
for (j
= 1; j
< i
; j
++)
nums
[i
][j
] = nums
[i
- 1][j
- 1] + nums
[i
- 1][j
];
}
for (i
= 0; i
< length
; i
++)
{
for (j
= 0; j
< length
- i
- 1; j
++)
cout
<< setw(3) << " ";
for (j
= 0; j
<= i
; j
++)
cout
<< setw(6) << nums
[i
][j
];
cout
<< endl
;
}
for (int l
= 0; l
< length
; l
++)
{
delete
[] nums
[l
];
}
delete
[] nums
;
}
转载请注明原文地址: https://yun.8miu.com/read-20908.html