<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="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b></font></span>
<font size="6" color="#FF0000"><b>Number Divider</b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is the first<span lang="en-ca"> </span>edition of my Number Divider<span lang="en-ca">. It </span>is rather a small program and the function is very simple--</b></pre>
</div>
<div align="left">
  <pre><b>giving out all possible ways of sum of a number. For example, there are 7 different ways to form number 5:</b></pre>
</div>
<div align="left">
  <pre><b>1 1 1 1 1
1 1 1 2
1 1 3
1 2 2
1 4
2 3
5</b></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><b>A number n can simply be summed up by 1 till n terms: say 5 can be divided into from 1 term up to 5 terms such </b></pre>
</div>
<div align="left">
  <pre><b>that the sum of all terms are equal to 5. We eliminated 0 terms which is useless in practical life. So, I just </b></pre>
</div>
<div align="left">
  <pre><b>write a recursive function to do the &quot;partition&quot; from 1 up to n. </b></pre>
</div>
<div align="left">
  <pre><b>And in order not to give repeating ways, all number must be in a ascending order. To keep them in this order, </b></pre>
</div>
<div align="left">
  <pre><b>I have to guarantee all terms would not be smaller than terms before. Every time, I subtract 1 from last term </b></pre>
</div>
<div align="left">
  <pre><b>and add to previous term unless ascending order cannot hold. So, the difference between last term and its preceding</b></pre>
</div>
<div align="left">
  <pre><b>term must be at least 2, right? This seems to be correct. But my mistake just arise from here. even the difference </b></pre>
</div>
<div align="left">
  <pre><b>is 1, it can be OK as long as this one(1) can be added into some term before. So, I write an awkward method to find </b></pre>
</div>
<div align="left">
  <pre><b>this preceding term. </b></pre>
</div>
<div align="left">
  <pre><b>3¡£ Major function</b></pre>
  <pre><b><span lang="en-ca">A. </span>void doPartition(int number, int divided);</b></pre>
  <pre><b>If the difference between last term and its preceding term is bigger than 1, then simply subtract one from last term and add </b></pre>
  <pre><b>it to its preceding term, then recursively call itself with number minus last term and size minus one. </b></pre>
  <pre><b>But if the difference is exactly one, it can only recursively call itself unless a previous term can be found such</b></pre>
  <pre><b>that it is smaller then its sequent term. </b></pre>
  <blockquote>
    <blockquote>
  <pre><b> </b></pre>
    </blockquote>
  </blockquote>
  <pre><b>4¡£ Further improvement£º</b></pre>
  <pre><b>What is use of this class?</b></pre>
  <pre><span lang="en-ca"><b>	</b></span></pre>
</div>
<pre>#include &lt;iostream&gt;

using namespace std;

const int MaxLength = 50;

class Partition
{
private:
	int lst[MaxLength];
	int length;
	void setLength(int newLength) { length = newLength;}
public:
	void initialize(int number, int divided);
	void doPartition(int number, int divided);
	void partition(int number);
	void display();
};

int counter=0;

int main()
{
	Partition P;
	P.partition(10);
	cout&lt;&lt;&quot;\nThe total is:&quot;&lt;&lt;counter&lt;&lt;endl;
	return 0;
}

void Partition::initialize(int number, int divided)
{
	for (int i=0;i &lt;divided;i++)
	{
		lst[i] = 1;
	}
	lst[divided-1] = number -divided+1;
}

void Partition::display()
{
	counter++;
	for (int i=0; i&lt;length; i++)
	{
		cout&lt;&lt;lst[i]&lt;&lt;&quot;  &quot;;
	}
	cout&lt;&lt;endl;
}


void Partition::partition(int number)
{
	for (int i=0;i&lt;number; i++)
	{
		lst[i]= 1;
	}
	for (i =number; i&gt;0; i--)
	{
		setLength(i);
		initialize(number, i);
		doPartition(number, i);
	}
}
	

void Partition::doPartition(int number, int divided)
{
	display();
	while (lst[divided-1] &gt; lst[divided-2]&amp;&amp;divided&gt;=2)
	{
		if (lst[divided-1]-lst[divided-2]&gt;1)
		{
			lst[divided-2]++;
			lst[divided-1]--;
		}
		else
		{
			int index = divided-2;
			while (index&gt;0&amp;&amp;lst[index]==lst[index-1])
			{
				index--;
			}
			if (index&gt;0)
			{
				lst[divided-1]--;
				lst[index-1]++;
			}
			else
			{
				break;
			}
		}
		doPartition(number - lst[divided-1], divided-1);		
	}	
}


	



</pre>

<p><span lang="en-ca"><font color="#FF0000"><a name="result"></a>The following 
is the partition:</font></span></p>

<p>1 1 1 1 1 1 1 1 1 1<br>
1 1 1 1 1 1 1 1 2<br>
1 1 1 1 1 1 1 3<br>
1 1 1 1 1 1 2 2<br>
1 1 1 1 1 1 4<br>
1 1 1 1 1 2 3<br>
1 1 1 1 2 2 2<br>
1 1 1 1 1 5<br>
1 1 1 1 2 4<br>
1 1 1 1 3 3<br>
1 1 1 2 2 3<br>
1 1 2 2 2 2<br>
1 1 1 1 6<br>
1 1 1 2 5<br>
1 1 1 3 4<br>
1 1 2 2 4<br>
1 1 2 3 3<br>
1 2 2 2 3<br>
2 2 2 2 2<br>
1 1 1 7<br>
1 1 2 6<br>
1 1 3 5<br>
1 2 2 5<br>
1 2 3 4<br>
2 2 2 4<br>
2 2 3 3<br>
1 1 8<br>
1 2 7<br>
1 3 6<br>
2 2 6<br>
2 3 5<br>
2 4 4<br>
3 3 4<br>
1 9<br>
2 8<br>
3 7<br>
4 6<br>
5 5<br>
10<br>
<br>
The total is:39<br>
¡¡</p>

<p>¡¡</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>