输出一个字符串的组合

#include<iostream>
#include<vector>
using namespace std;

void combination(const char str[],int num,vector<char> &result);

void combination(const char str[])
{
    if(str == NULL)
        return;
    int len = strlen(str);
    int i=0;
    vector<char> result;
    for(i=1; i<=len; i++)
        combination(str,i,result);
}

void combination(const char str[],int num,vector<char> &result)
{
    if(NULL == str)
        return;
    if(num == 0)
    {
        vector<char>::iterator it;
        for(it=result.begin(); it!=result.end(); it++)
            cout<<*it;
        cout<<endl;
        return;
    }

    if(*str=='\0')
        return;

    int i = 0;
    int len = strlen(str);
    //choose the current element
    result.push_back(*str);
    combination(str+1,num-1,result);
    result.pop_back();
    //not choose
    combination(str+1,num,result);
    
}

int main()
{
    char str[100];
    while(cin>>str)
    {
        combination(str);
    }
    
    return 0;
}

 

posted @ 2013-03-13 21:34  Frank@609  Views(210)  Comments(0Edit  收藏  举报