<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><span lang="en-ca"><font size="6" color="#FF0000"><b>
Generator(1)</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is the <span lang="en-ca"> first </span>edition of my </b><span lang="en-ca"><b>my Permutation Generator class. It is almost like a copy  from the algorithm </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>from &quot;Discrete Mathematics&quot; into C++. I just do like a translating machine, cause I don't understand clearly </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>about what is doing there. Just to release the boredness of life. I am really feeling a low-morale without </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>coding. Later I add algorithm of generating combinations</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Idea of program</b></font></span></pre>
</div>
<div align="left">
  <pre><b>1¡£ Basic idea: </b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>What else can a thief talk about the thing that he stole from others?</b></span></pre>
</div>
<div align="left">
  <pre><b>2¡£ Program design: </b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">I just make it a class and permutation is nothing but a sequence, so I give out the sequence index for the object to make a</span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">&quot;sequential permutation&quot; (awkward?). I plan to give out my index array instead to handle the customer's object list. </span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">Actually when customer get the index, he know how to do it. Therefore, I finish my job. </span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">As for combination, you can see that each index of array is 1 for the element is in the set, or 0 for it is not. So, it is </span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca">simple. and do you feel cheated?</span></pre>
</div>
<div align="left">
  <pre><b>3¡£ Major function</b></pre>
  <pre><span lang="en-ca">A. </span>void slotMachine();</pre>
  <pre><span lang="en-ca">I like the name.</span></pre>
  <blockquote>
    <blockquote>
  <pre><b> </b></pre>
    </blockquote>
  </blockquote>
  <pre><b>4¡£ Further improvement£º</b></pre>
  <pre><b>	A. <span lang="en-ca">I was in a hurry as there is a final exam of Probability this evening and I just feel extremely </span></b></pre>
  <pre><span lang="en-ca"><b>boring to be irresistable to do a little coding job. </b></span></pre>
  <pre><span lang="en-ca"><b>	</b></span></pre>
</div>
<pre>#include &lt;iostream&gt;

using namespace std;

const int MaxNumber =100;

class Generator
{
private:
	bool isCombination;
	int array[MaxNumber];
	int length;
	void nextGenerator();
	int nextDigit();
	int findSmallest(int next);
	void swapIndex(int a, int b);
	void formTail(int next);
	void output();
	void initialize(int number);
protected:
	void doGenerator();
	void doCombination();
public:
	Generator(int number =10, bool combination=false);
	void setLength(int number);
	int getLength() { return length;}	
	void slotMachine();
};


int main()
{
	Generator P(6);
	P.slotMachine();

	return 0;
}


void Generator::doCombination()
{
	int index =0;
	while (true)
	{
		while (array[index])
		{
			array[index] = 0;
			index++;
			if (index==length)
				return;
		}
		array[index] = 1;
		output();
		index =0;
	}
}

void Generator::doGenerator()
{
	int next =0;
	int small =0;
	while ((next = nextDigit()) != -1)
	{
		small = findSmallest(next);
		swapIndex(next, small);
		formTail(next);
		output();
	}
}

Generator::Generator(int number, bool combination)
{
	setLength(number);
	isCombination = combination;
	initialize(number);
}


void Generator::output()
{
	if (!isCombination)
	{
		cout&lt;&lt;&quot;\nThis is the Permutation:\n&quot;;
	
	}
	else
	{
		cout&lt;&lt;&quot;\nThis is the Combination:\n&quot;;
	}
	for (int i=0; i&lt; length; i++)
	{
		cout&lt;&lt;array[i]&lt;&lt;(i==length - 1?&quot;;&quot;:&quot;,&quot;);
	}
}


void Generator::slotMachine()
{
	output();
	if (!isCombination)
	{
		doGenerator();
	}
	else
	{
		doCombination();
	}
}



void Generator::formTail(int next)
{
	int start = next +1;
	int end = length -1;
	while (end &gt; start)
	{
		swapIndex(start, end);
		start++;
		end--;
	}
}

void Generator::swapIndex(int a, int b)
{
	int temp;
	temp = array[a];
	array[a] = array[b];
	array[b] = temp;
}

int Generator::findSmallest(int next)
{
	int small = length -1;
	while (array[next]&gt; array[small])
	{
		small--;
	}
	return small;
}


void Generator::setLength(int number)
{
	if (number&gt;1)
	{
		 length = number;		 
	}
}




int Generator::nextDigit()
{
	int next = length -2;
	while (array[next]&gt; array[next+1])
	{
		next--;
		if (next&lt;0)
		{
			return -1;
		}
	}
	return next;
}


void Generator::initialize(int number)
{
	if (!isCombination)
	{
		for (int i=0; i&lt; length; i++)
		{
			array[i]= i;
		}
	}
	else
	{
		for (int i=0; i&lt; length; i++)
		{
			array[i]= 0;
		}
	}
}

</pre>
<pre></pre>
<pre></pre>
<pre></pre>

<p>¡¡          


</p>

<pre>This is the permutation:
0,1,2,3;
This is the permutation:
0,1,3,2;
This is the permutation:
0,2,1,3;
This is the permutation:
0,2,3,1;
This is the permutation:
0,3,1,2;
This is the permutation:
0,3,2,1;
This is the permutation:
1,0,2,3;
This is the permutation:
1,0,3,2;
This is the permutation:
1,2,0,3;
This is the permutation:
1,2,3,0;
This is the permutation:
1,3,0,2;
This is the permutation:
1,3,2,0;
This is the permutation:
2,0,1,3;
This is the permutation:
2,0,3,1;
This is the permutation:
2,1,0,3;
This is the permutation:
2,1,3,0;
This is the permutation:
2,3,0,1;
This is the permutation:
2,3,1,0;
This is the permutation:
3,0,1,2;
This is the permutation:
3,0,2,1;
This is the permutation:
3,1,0,2;
This is the permutation:
3,1,2,0;
This is the permutation:
3,2,0,1;
This is the permutation:
3,2,1,0;</pre>

<p>¡¡</p>

<p><br>
This is the Combination:<br>
0,0,0,0;<br>
This is the Combination:<br>
1,0,0,0;<br>
This is the Combination:<br>
0,1,0,0;<br>
This is the Combination:<br>
1,1,0,0;<br>
This is the Combination:<br>
0,0,1,0;<br>
This is the Combination:<br>
1,0,1,0;<br>
This is the Combination:<br>
0,1,1,0;<br>
This is the Combination:<br>
1,1,1,0;<br>
This is the Combination:<br>
0,0,0,1;<br>
This is the Combination:<br>
1,0,0,1;<br>
This is the Combination:<br>
0,1,0,1;<br>
This is the Combination:<br>
1,1,0,1;<br>
This is the Combination:<br>
0,0,1,1;<br>
This is the Combination:<br>
1,0,1,1;<br>
This is the Combination:<br>
0,1,1,1;<br>
This is the Combination:<br>
1,1,1,1;</p>

<p><span lang="en-ca"> <br>
¡¡</span></p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="Monopoly.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>