贪心法——最优装载问题

贪心法——最优装载问题

最优装载问题。给出 n 个物体,第i个物体重量为 wi 。选择尽量多的物体,使得总重量不超过 C <script id="MathJax-Element-230" type="math/tex">C</script>。

只关心物体的数量,这样只需把所有物体按重量从小到大排序,依次选取每个物体,直到装不下为止。这就是一种典型的贪心算法,只顾眼前,但在一定程度上却能得到最优解。

最优装载问题实现算法

// 贪心法
// 最优装载问题
void optionalLoad(int *a, int n, int C) {
    sort(a, a + n);
    int retain = C;
    for(int i = 0; i < n; i++) {
        if(a[i] <= retain) {
            cout << a[i] << " ";
            retain -= a[i];
        }
    }
    cout << endl;
}

测试主程序

#include <iostream>
#include <algorithm>

using namespace std;

// 贪心法
// 最优装载问题
void optionalLoad(int *a, int n, int C) {
    sort(a, a + n);
    int retain = C;
    for(int i = 0; i < n; i++) {
        if(a[i] <= retain) {
            cout << a[i] << " ";
            retain -= a[i];
        }
    }
    cout << endl;
}

int main() {
    while(true) {
        // n个物体
        int n;
        cout << "请输入物体总数(0退出):";
        cin >> n;
        if(!n) {
            break;
        }
        int C;
        cout << "请输入不超过的总重量:";
        cin >> C;
        int a[n];
        cout << "分别输入" << n << "个物体的重量:";
        for(int i = 0; i < n; i++) {
            cin >> a[i];
        }
        cout << "最优装载为:" << endl;
        optionalLoad(a, n, C);
    }
    return 0;
}

输出数据

请输入物体总数(0退出):5
请输入不超过的总重量:10
分别输入5个物体的重量:5 3 2 4 1
最优装载为:
1 2 3 4
请输入物体总数(0退出):3
请输入不超过的总重量:3
分别输入3个物体的重量:5 7 4
最优装载为:

请输入物体总数(0退出):2
请输入不超过的总重量:10
分别输入2个物体的重量:2 6
最优装载为:
2 6
请输入物体总数(0退出):0

Process returned 0 (0x0)   execution time : 31.096 s
Press any key to continue.
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
贪心算法是一种常用的解决最优装载问题算法。在Java中,可以使用贪心算法来解决最优装载问题最优装载问题是指在给定一组物品和一个容量限制的背包时,如何选择物品放入背包中,使得背包的总价值最大化。贪心算法的思想是每次选择当前最优的物品放入背包中,直到背包无法再放入更多物品为止。 以下是Java中贪心算法解决最优装载问题的基本步骤: 1. 定义一个物品类,包含物品的重量和价值属性。 2. 根据物品的价值重量比进行排序,从大到小排序。 3. 初始化背包的容量和总价值为0。 4. 遍历排序后的物品列表,依次将物品放入背包中,直到背包无法再放入更多物品或者所有物品都已经放入背包为止。 5. 返回背包的总价值作为最优解。 下面是一个简单的Java代码示例: ```java import java.util.Arrays; class Item implements Comparable<Item> { int weight; int value; public Item(int weight, int value) { this.weight = weight; this.value = value; } @Override public int compareTo(Item other) { double ratio1 = (double) this.value / this.weight; double ratio2 = (double) other.value / other.weight; if (ratio1 > ratio2) { return -1; } else if (ratio1 < ratio2) { return 1; } else { return 0; } } } public class GreedyAlgorithm { public static int knapsack(Item[] items, int capacity) { Arrays.sort(items); int totalValue = 0; int currentWeight = 0; for (Item item : items) { if (currentWeight + item.weight <= capacity) { currentWeight += item.weight; totalValue += item.value; } else { int remainingCapacity = capacity - currentWeight; totalValue += item.value * ((double) remainingCapacity / item.weight); break; } } return totalValue; } public static void main(String[] args) { Item[] items = {new Item(10, 60), new Item(20, 100), new Item(30, 120)}; int capacity = 50; int maxValue = knapsack(items, capacity); System.out.println("最优装载问题最大价值为:" + maxValue); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值