对如输入字符串 a,b, 输出 aa ab ba bb, 按字典输出顺序

// 对如输入字符串 a,b, 输出 aa ab ba bb, 按字典输出顺序.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <list>

using namespace std;



void print_list(std::list<char>::iterator &itr1, std::list<char>::iterator &itr2)
{
	std::list<char>::iterator itr = itr1;

	while (itr != itr2)
	{
		printf("%c ", (*itr));
		++itr;
	}
	printf("\n");
}


void foo(char A[], int len, int leftSpace, std::list<char> &mList)
{
	if (leftSpace == 0)
	{
		print_list(mList.begin(), mList.end());
		return;
	}

	for(int idx = 0; idx < len; idx++)
	{
		mList.push_back(A[idx]);
		foo(A, len, leftSpace-1, mList);
		mList.pop_back();
	}
}

void printAllList(char A[], int len)
{
	if (A == NULL || len == 0)
		return;

	std::list<char> mList;

	for (int idx = 0; idx < len; idx++)
	{
		mList.push_back(A[idx]);

		foo(A, len, len-1, mList);

		mList.pop_back();
	}
}


int _tmain(int argc, _TCHAR* argv[])
{
	char A[] = {"ABC"};


	printAllList(A, strlen(A));
	return 0;
}

你可能感兴趣的:(对如输入字符串 a,b, 输出 aa ab ba bb, 按字典输出顺序)