杨辉三角函数(C++实现)

杨辉三角函数(C++实现)

#include<iostream>
#include<vector>
using namespace std;
void f(int numRows,int k) {
	vector<vector<int>> ans(numRows);
	for (int i = 0; i < numRows; ++i) {
		ans[i] = vector<int>(i + 1, 1);
	}

	for (int i = 2; i < numRows; ++i) {
		for (int j = 1; j < i; ++j) {
			ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j];
		}
	}
	for (int i = 0; i < ans.size(); i++)
	{

		for (int j = 0; j < ans[i].size(); j++)
		{

			cout << ans[i][j];
		}
		cout << endl;
	}
	for (int i = 0; i < ans[k].size(); i++)
	{
		cout << ans[k][i] << " ";
	}


}
int main()
{
	int numRows;
	cout << "请输入列数" << endl;
	cin >> numRows;
	cout << "请输入要输出的行数" << endl;
	int k;
	cin >> k;
	f(numRows, k);
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页