杨辉三角

#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;
}

 

posted on 2016-03-26 13:56  各种笔记  阅读(436)  评论(0)    收藏  举报