<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>Pocket Ruler</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><span lang="en-ca"><b>&nbsp; 
 
</b></span><b>&nbsp;&nbsp;&nbsp; </b></font><span lang="en-ca">
<font size="6" color="#FF0000"><b>Pocket Ruler</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><span lang="en-ca"><b>This is another example of dynamic programming which is the question of assignment of comp465.</b></span><b><span lang="en-ca"> </span></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <font FACE="CMR12">
  <p ALIGN="LEFT">The problem called FOLDING RULER goes as follows. Given</p>
  <p ALIGN="LEFT">a ruler made of rigid rods connected end-to-end by hinges 
  (much as a carpenter¡¯s</p>
  <p ALIGN="LEFT">ruler, except that dierent rods may have dierent lengths), can 
  you</p>
  <p ALIGN="LEFT">fold it to make it fit in your pocket?</p>
  <p ALIGN="LEFT">Formally, the input is a sequence of positive integers </font>
  <font FACE="CMMI12">a</font><font FACE="CMR8" SIZE="1">1</font><font FACE="CMMI12">, 
  a</font><font FACE="CMR8" SIZE="1">2</font><font FACE="CMMI12">, . . . , a</font><font FACE="CMMI8" SIZE="1">n
  </font><font FACE="CMR12">(the</p>
  <p ALIGN="LEFT">lengths of the </font><font FACE="CMMI12">n </font>
  <font FACE="CMR12">consecutive rods) and a positive integer </font>
  <font FACE="CMMI12">b </font><font FACE="CMR12">(the depth of</p>
  <p ALIGN="LEFT">your pocket); each way of folding the ruler can be represented 
  by a sequence</p>
  </font><font FACE="CMMI12">
  <p ALIGN="LEFT">x</font><font FACE="CMR8" SIZE="1">1</font><font FACE="CMMI12">, 
  x</font><font FACE="CMR8" SIZE="1">2</font><font FACE="CMMI12">, . . . , x</font><font FACE="CMMI8" SIZE="1">n
  </font><font FACE="CMR12">such that each </font><font FACE="CMMI12">x</font><font FACE="CMMI8" SIZE="1">i
  </font><font FACE="CMR12">is +1 (meaning that the </font><font FACE="CMMI12">i</font><font FACE="CMR12">-th 
  rod points up) or</p>
  </font><font FACE="CMSY10">
  <p ALIGN="LEFT">-</font><font FACE="CMR12">1 (meaning that the </font>
  <font FACE="CMMI12">i</font><font FACE="CMR12">-th rod points down); the ruler 
  folded in this particular</p>
  <p ALIGN="LEFT">way will fit in your pocket if and only if there is an integer
  </font><font FACE="CMMI12">s </font><font FACE="CMR12">(representing the</p>
  <p ALIGN="LEFT">position in your pocket where the beginning point of the ruler 
  finds itself)</p>
  <p ALIGN="LEFT">such that 0 <span lang="en-ca">&lt;=</span></font><font FACE="CMSY10">
  </font><font FACE="CMMI12">s</font><font FACE="CMSY10"><span lang="en-ca"> &lt;=</span>
  </font><font FACE="CMMI12">b </font><font FACE="CMR12">and</p>
  <p ALIGN="LEFT">0 </font><font FACE="CMSY10"><span lang="en-ca">&lt;=</span>
  </font><font FACE="CMMI12">s </font><font FACE="CMR12">+</font><font FACE="CMMI12"><span lang="en-ca">&nbsp; 
  sum(</span>x</font><font FACE="CMMI8" SIZE="1">i</font><font FACE="CMMI12">a</font><font FACE="CMMI8" SIZE="1">i</font><font FACE="CMR12"><span lang="en-ca">) 
  &lt;=b f</span>or all choices of </font><font FACE="CMMI12">k </font>
  <font FACE="CMR12">= 0</font><font FACE="CMMI12">, </font><font FACE="CMR12">1</font><font FACE="CMMI12">, 
  . . . , n.</p>
  </font><font FACE="CMR12">
  <p ALIGN="LEFT">(For example, the ruler whose sequence of rod lengths is 
  9,7,3,8 will fit into</p>
  <p ALIGN="LEFT">a pocket of depth 10: consider </font><font FACE="CMMI12">s
  </font><font FACE="CMR12">= 1</font><font FACE="CMMI12">, x</font><font FACE="CMR8" SIZE="1">1
  </font><font FACE="CMR12">= +1</font><font FACE="CMMI12">, x</font><font FACE="CMR8" SIZE="1">2
  </font><font FACE="CMR12">= </font><font FACE="CMSY10">-</font><font FACE="CMR12">1</font><font FACE="CMMI12">, 
  x</font><font FACE="CMR8" SIZE="1">3 </font><font FACE="CMR12">= </font>
  <font FACE="CMSY10">-</font><font FACE="CMR12">1</font><font FACE="CMMI12">, x</font><font FACE="CMR8" SIZE="1">4
  </font><font FACE="CMR12">= +1.)</p>
  <p ALIGN="LEFT">Design a dynamic programming algorithm for solving this 
  problem and</p>
  <p ALIGN="LEFT">present it the pseudo code style of Question 1, Homework 
  Assignment 3.</p>
  </font>
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  ¡¡</div>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>My experience is that 
dynamic programming is simply an variation of recursion. Therefore it is always 
helpful </b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>if you can figure out the 
recursive version of program first. </b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>The other thing apart from 
efficiency I don't like about dynamic programming is that it is very difficult 
to </b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>find the path. In this 
problem, I cannot find the solution for the question except a Boolean answer.</b></font></span></p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre>¡¡</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre>¡¡</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b><span lang="en-ca">1. Ruler.cpp</span></b></font></pre>
</div>
<div align="left">
  <pre>¡¡</pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: LCS.cpp</b></font></span></pre>
</div>
<pre>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

const int RulerNumber=15;
const int MaxHeight=100;


int positions[RulerNumber][MaxHeight];//0: unknown, 1: ok, -1 no;

int rulers[RulerNumber];
//Record records[MaxLengthNumber*2];

int choices[RulerNumber];//either 1 or -1

void initialize();
void display();

void dynaFind();
void statFind();

bool findRuler(int step, int current);

int height;

int main()
{
	initialize();
	display();
	printf(&quot;input the length you want:&quot;);
	scanf(&quot;%d&quot;, &amp;height);
	
	statFind();
	dynaFind();
	return 0;
}

void dynaFind()
{	
	int index;
	//bool secondTry;
	for (int step=0; step&lt;RulerNumber; step++)
	{
		for (int cur=0; cur&lt;height; cur++)
		{
			if (step==0)
			{
				if (cur==height-rulers[0])
				{
					positions[step][cur]=1;
				}
				else
				{
					positions[step][cur]=-1;
				}
			}
			else
			{
				
				positions[step][cur]=-1;
				//not exceeds bound
				index=cur-rulers[step];
				if (index&gt;=0)
				{
					//last is ok
					if (positions[step-1][index]==1)
					{
						if (step==RulerNumber-1)
						{
							printf(&quot;\nthere is a solution for dynamic programming.\n&quot;);
							return;
						}
						positions[step][cur]=1;						
					}				
				}
				if (positions[step][cur]==-1)
				{
					//not exceeds bound
					index=cur+rulers[step];
					if (index&lt;height)
					{
						//if last row is ok
						if (positions[step-1][index]==1)
						{
							if (step==RulerNumber-1)
							{
								printf(&quot;\nthere is a solution for dynamic programming.\n&quot;);
								return;
							}
							positions[step][cur]=1;
						}					
					}				
				}
			}
		}
	}
}

void statFind()
{
	int current;
	if (findRuler(0, height))
	{
		printf(&quot;the choices is \n&quot;);
		current=height;

		for (int i=0; i&lt;RulerNumber; i++)
		{
			//printf(&quot;choices[%d]=%d\n&quot;, i, choices[i]);
			printf(&quot;step%d: %d and position is: &quot;, i, choices[i]);
			current+=choices[i]*rulers[i];
			printf(&quot;%d\n&quot;, current);
		}
	}
	else
	{
		printf(&quot;no solution\n&quot;);
	}
}

void initialize()
{
	for (int i=0; i&lt;RulerNumber; i++)
	{
		rulers[i]=10+rand()%50;
		for (int j=0; j&lt;MaxHeight; j++)
		{
			positions[i][j]=0;
		}
	}

}

void display()
{
	for (int i=0; i&lt;RulerNumber; i++)
	{
		printf(&quot;rulers[%d]=%d\n&quot;, i, rulers[i]);
	}
}

bool findRuler(int step, int current)
{	
	if (current&gt;height||current&lt;0)
	{
		return false;
	}
	else
	{
		if (step==RulerNumber-1)
		{
			return true;
		}
	}
	
	if (findRuler(step+1, current-rulers[step]))
	{
		choices[step]=-1;
		return true;
	}
	else
	{
		if (findRuler(step+1, current+rulers[step]))
		{
			choices[step]=1;
			return true;
		}
		else
		{
			return false;
		}
	}
}

</pre>
<pre></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>How to run?</b></font></span></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>You see the combination of recursion version. But for the dynamic programming version, there is only a true or </b></font></span></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>false answer.</b></font></span></pre>
<pre><span lang="en-ca"> </span></pre>
<pre>rulers[0]=51
rulers[1]=27
rulers[2]=44
rulers[3]=10
rulers[4]=29
rulers[5]=34
rulers[6]=38
rulers[7]=18
rulers[8]=22
rulers[9]=24
rulers[10]=15
rulers[11]=55
rulers[12]=41
rulers[13]=37
rulers[14]=21
input the length you want:79
the choices is
step0: -1 and position is: 28
step1: -1 and position is: 1
step2: 1 and position is: 45
step3: -1 and position is: 35
step4: -1 and position is: 6
step5: 1 and position is: 40
step6: -1 and position is: 2
step7: 1 and position is: 20
step8: 1 and position is: 42
step9: -1 and position is: 18
step10: -1 and position is: 3
step11: 1 and position is: 58
step12: -1 and position is: 17
step13: 1 and position is: 54
step14: 0 and position is: 54

there is a solution for dynamic programming.
Press any key to continue
</pre>

<pre></pre>

<pre></pre>

<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; <a href="game24.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>