首页 > 其他 > 详细

杨辉三角

时间:2016-03-26 15:17:25      阅读:240      评论:0      收藏:0      [点我收藏+]
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;


class Lab1 {
private:
    vector<vector<int>> whole;
public:
    Lab1(int n) {
        vector<int> tmpVec;
        tmpVec.push_back(1);
        whole.push_back(tmpVec);
        tmpVec.clear();
        for(int i=1;i<n;i++){
            int tmp=0;
            for(vector<int>::iterator it=whole[i-1].begin();it!=whole[i-1].end();it++){
                tmpVec.push_back(tmp+(*it));
                tmp=*it;
            }
            tmpVec.push_back(1);
            whole.push_back(tmpVec);
            tmpVec.clear();
        }
    }
    void printWhole(){
        cout<<"["<<endl;
        for(vector<vector<int>>::iterator it=whole.begin();it!=whole.end();it++){
            cout<<"[";
            for(vector<int>::iterator t=it->begin();t!=it->end();t++){
                cout<<*t<<((t!=it->end()-1)?",":"");
            }
            cout<<"]"<<endl;
            cout<<endl;
        }
        cout<<"]"<<endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Lab1 *a=new Lab1(6);
    a->printWhole();
    return 0;
}

 

杨辉三角

原文:http://www.cnblogs.com/kateblog/p/5322803.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!