<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>潘锦的空间 &#187; 装箱问题</title>
	<atom:link href="https://www.phppan.com/tag/%e8%a3%85%e7%ae%b1%e9%97%ae%e9%a2%98/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sat, 23 May 2026 03:04:48 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>装箱问题</title>
		<link>https://www.phppan.com/2009/08/bin-packing-problem/</link>
		<comments>https://www.phppan.com/2009/08/bin-packing-problem/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 13:27:27 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[程序相关]]></category>
		<category><![CDATA[ACM]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[dynamic programming]]></category>
		<category><![CDATA[动态规划]]></category>
		<category><![CDATA[装箱问题]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=106</guid>
		<description><![CDATA[描述 Description 有一个箱子容量为v(正整数，o≤v≤20000)，同时有n个物品(o≤n≤30) [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><strong>描述 Description   </strong><br />
有一个箱子容量为v(正整数，o≤v≤20000)，同时有n个物品(o≤n≤30)，每个物品有一个体积 (正整数)。要求从 n 个物品中，任取若千个装入箱内，使箱子的剩余空间为最小。</p>
<p><strong> 输入格式 Input Format  </strong><br />
    第一行，一个整数，表示箱子容量；<br />
第二行，一个整数，表示有n个物品；<br />
接下来n行，分别表示这n个物品的各自体积。  </p>
<p><strong> 输出格式 Output Forma</strong>t<br />
   一个整数，表示箱子剩余空间。 </p>
<p><strong> 样例输入 Sample Input </strong><br />
24<br />
6<br />
8<br />
3<br />
12<br />
7<br />
9<br />
7<br />
<strong><br />
 样例输出 Sample Output </strong><br />
   0<br />
<strong>算法描述：动态规划</strong>。<br />
一个简单的动态规划问题-01背包<br />
f[i][j]=max{f[i-1][j],f[i-1][j-g[i]+g[i](j-g[i]>=0)}<br />
f[0][j]=0<br />
逐行递推答案即为(V-f[n][v])<br />
我没有用这种方法。二维太复杂了，写了个一维的。就是一个一个的放，放在以前存在的情况上，然后相加，注意这里最重要的一点是要从后面往前面找那个存在的。<br />
【代码】</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> a<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> v<span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> j<span style="color: #339933;">,</span> k<span style="color: #339933;">,</span> b<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">20001</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> t<span style="color: #339933;">;</span>
	<span style="color: #000066;">scanf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d%d&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>v<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000066;">scanf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">memset</span><span style="color: #009900;">&#40;</span>b<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	k <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	b<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		t <span style="color: #339933;">=</span> k<span style="color: #339933;">;</span>
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> k<span style="color: #339933;">;</span> j <span style="color: #339933;">&gt;=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span><span style="color: #666666; font-style: italic;">//切记一定要从后面往前面找。                   </span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">&amp;&amp;</span> j <span style="color: #339933;">+</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> v<span style="color: #009900;">&#41;</span>                                                                     
			<span style="color: #009900;">&#123;</span>
				b<span style="color: #009900;">&#91;</span>j <span style="color: #339933;">+</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">+</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> t<span style="color: #009900;">&#41;</span>
					t <span style="color: #339933;">=</span> j <span style="color: #339933;">+</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		k <span style="color: #339933;">=</span> t<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> v <span style="color: #339933;">-</span> k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/08/bin-packing-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
