# 一个递归转循环的模式

void Recursive_Subsets(int* a, bool* b, int start, int end)
{
if (start <= end)
{
// determine current number's status, which has 2 choices
// and then continue with left ones
b[start] = true;
Recursive_Subsets(a, b, start+1, end);

b[start] = false;
Recursive_Subsets(a, b, start+1, end);
}
else
{
for (int i = 0; i <= end; i++)
{
if (b[i]) cout << a[i];
}
cout << endl;
}

}

void PrintAllSubsets1(int* a, int n)
{
bool* b = new bool[n];
Recursive_Subsets(a, b, 0, n-1);
delete b;
}

void PrintAllSubsets2(int* a, int n)
{
// Initialize flags as false
bool* b = new bool[n];
for(int i = 0; i < n; i++)
{
b[i] = false;
}

// Use 2 loops to enumerate all possible combinations of (each item's status * number of items),
// in this case: ([true|false] * size of set)
while(true)
{
// Get one solution, output it!
for(int i = 0; i < n; i++)
{
if(b[i]) cout << a[i];
}
cout << endl;

// One of the number's status has switched, start over enumeration for that!
// ex: I have following enumeration for 3 numbers:
//     0, 0, 0
//	   0, 0, 1
//	   0, 1, 0
//	   0, 1, 1
// now if I changed the first number's status from 0 to 1, I need to re-enumerate the other 2 numbers
// to get all possible cases:
//     1, 0, 0
//	   1, 0, 1
//	   1, 1, 0
//	   1, 1, 1
int k = n - 1;

while(k >= 0)
{
if(b[k] == false)	// have we tried all possible status of k?
{
b[k] = true;	// status switch, as we only have 2 status here, I use a boolean rather than an array.
break;			// break to output the updated status, and further enumeration for this status change, just like a recursion
}
else				// We have tried all possible cases for k-th number, now let's consider the previous one
{
b[k] = false;	// resume k to its initial status
k--;			// let's consider k-1
}
}

if(k < 0) break;		// all the numbers in the set has been processed, we are done!
}

// clean up
delete [] b;
}


An Introduction to Recursion, Part 1

An Introduction to Recursion, Part 2

posted @ 2011-02-12 08:18  lzprgmr  阅读(10693)  评论(8编辑  收藏  举报