c++ 实现整数的拆分

来源:互联网 发布:阿里云电视系统升级 编辑:程序博客网 时间:2024/04/30 20:53

// interDived.cpp : Defines the entry point for the console application.
//Writed by Johsnon Chen 2010/10/12

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
vector<int> iVec;

void split_int(int n, int base)
{
    if(n == 0)
    {
        cout << "find a combination: ";
        for(vector<int>::size_type i= 0; i < iVec.size(); ++i)
            cout << iVec[i] << " ";
        cout << endl;
        return;
    }
    else
    {
        for(int i = base + 1; i <= n; i++)
        {
            iVec.push_back(i);
            split_int(n - i, i);
            iVec.erase(--iVec.end());
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    split_int(6, 0);
    return 0;
}