动态规划 - 背包问题

package genius.base;
public class PackageAnswer {
    /**
     * @param m 表示背包的最大容量
     * @param n 表示商品的个数 
     * @param w 表示商品重量数组 weight
     * @param p 表示商品价值数组 price
     */
    public static int[][] BackPack(int m, int n, int[] w, int[] p) {
        //c[i][v]表示前i件物品恰放入一个重量为m的背包可以获得的最大价值
        int c[][] = new int[n + 1][m + 1];
        /**
         * 关键是考虑在什么情况下要放入背包中。
         */
        for (int i = 1; i < n + 1; i++) {
            for (int j = 1; j < m + 1; j++) {
                //当物品为i件重量为j时,如果第i件的重量(w[i-1])小于重量j时,c[i][j]为下列两种情况之一:
                //(1)物品i不放入背包中,所以c[i][j]为c[i-1][j]的值
                //(2)物品i放入背包中,则背包剩余重量为j-w[i-1],所以c[i][j]为c[i-1][j-w[i-1]]的值加上当前物品i的价值
                if (w[i - 1] <= j) {
                    c[i][j] = Math.max(c[i - 1][j - w[i - 1]] + p[i - 1],c[i - 1][j]);
                } else
                    //相当于赋值更新
                    c[i][j] = c[i - 1][j];
            }
        }
        return c;
    }

    public static void main(String[] args) {
        int m = 10;
        int n = 3;
        int w[] = {2, 8, 6, 7};
        int p[] = {4, 5, 6, 2};
        int c[][] = BackPack(m, n, w, p);
        for (int i = 1; i <=n; i++) {
            for (int j = 1; j <=m; j++) {
                System.out.print(c[i][j]+"\t");
                if(j==m){
                    System.out.println();
                }
            }
        }
    }
}
0   4   4   4   4   4   4   4   4   4   
0   4   4   4   4   4   4   5   5   9   
0   4   4   4   4   6   6   10  10  10  
30 Ways to Have Some Computer-Controlled Evil Fun! “The steps are easy to follow…text is precise and understandable…uses very clear pictures and schematics to show what needs doing…Most importantly these projects are fun!”–Boing Boing This wickedly inventive guide shows you how to program and build a variety of projects with the Arduino microcontroller development system. Covering Windows, Mac, and Linux platforms, 30 Arduino Projects for the Evil Genius gets you up to speed with the simplified C programming you need to know–no prior programming experience necessary. Using easy-to-find components and equipment, this do-it-yourself book explains how to attach an Arduino board to your computer, program it, and connect electronics to it to create fiendishly fun projects. The only limit is your imagination! 30 Arduino Projects for the Evil Genius: Features step-by-step instructions and helpful illustrations Provides full schematic and construction details for every project Covers the scientific principles behind the projects Removes the frustration factor–all required parts are listed along with sources Build these and other devious devices: Morse code translator High-powered strobe light Seasonal affective disorder light LED dice Keypad security code Pulse rate monitor USB temperature logger Oscilloscope Light harp LCD thermostat Computer-controlled fan Hypnotizer Servo-controlled laser Lie detector Magnetic door lock Infrared remote Each fun, inexpensive Evil Genius project includes a detailed list of materials, sources for parts, schematics, and lots of clear, well-illustrated instructions for easy assembly. The larger workbook-style layout and convenient two-column format make following the step-by-step instructions a breeze. In December 2011, Arduino 1.0 was released. This changed a few things that have caused the sketches for Projects 10, 27, and 28 in this book to break. To fix this, you will need to get the latest versions of the Keypad and IRRemote libraries. The Keypad library has been updated for Arduino 1.0 by its original creators and can be downloaded from here: http://www.arduino.cc/playground/Code/Keypad Ken Shiriff’s IRRemote library has been updated and can be downloaded from here: http://www.arduinoevilgenius.com/new-downloads Make Great Stuff! TAB, an imprint of McGraw-Hill Professional, is a leading publisher of DIY technology books for makers, hackers, and electronics hobbyists. Book Details Series: Evil Genius Paperback: 208 pages Publisher: McGraw-Hill/TAB Electronics; 1 edition (July 28, 2010) Language: English ISBN-10: 007174133X ISBN-13: 978-0071741330
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值