Given numRows, generate the first numRows of Pascal‘s triangle.
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
#include <iostream> #include <vector> #include <string> using namespace std; void Pascaltriangle(int n) { vector<vector<int> > result; int i,j; for(i=0;i<n;i++) { vector<int> tmp; for(j=0;j<=i;j++) { if(j==0 || j == i) tmp.push_back(1); else tmp.push_back(result[i-1][j]+result[i-1][j-1]); } result.push_back(tmp); } for(i=0;i<result.size();i++) { for(j=0;j<result[i].size();j++) cout<<result[i][j]<<" "; cout<<endl; } } int main() { Pascaltriangle(5);; return 0; }
原文地址:http://blog.csdn.net/yusiguyuan/article/details/44955559