背包问题
来源:互联网 发布:windows api大全 编辑:程序博客网 时间:2024/06/08 00:16
#include<stdio.h>#include<stdlib.h>struct Node{ double value; double weight;}a[10];void quick_sort(struct Node *arr, int l, int r) //对结束时间进行排序{ if (l < r) //进行判断,用来终止递归 { int i = l, j = r, x = arr[l].value; struct Node temp = arr[l]; //用来交换结构体数组变量 while (i < j) { while (i < j && x <= arr[j].value) //从右往左找一个小于x的数 j--; if (i < j) arr[i++] = arr[j]; while (i < j && x > arr[i].value) //从左往右找一个大于x的数 i++; if (i < j) arr[j--]= arr[i]; } arr[i] = temp; quick_sort(arr, l ,i - 1); //递归调用,分治思想,左边分治。 quick_sort(arr, i + 1, r); //右边分治 }}int main(){ int s, i, j; scanf("%d",&s); while (s--) //s组测试数据 { int n; double m, total_value = 0, total_weight = 0; //m为背包所能装的最大重量 scanf("%d%lf",&n, &m); for (i = 0; i < n; i++) scanf("%lf%lf",&a[i].value, &a[i].weight); quick_sort(a, 0, n - 1); for (i = n - 1; i >= 0; i--) { if (total_weight + a[i].weight <= m) { total_weight += a[i].weight; total_value += a[i].value * a[i].weight; } else break; } total_value += (m - total_weight) * a[i].value; printf("%d\n",(int)total_value); } return 0;}
阅读全文
0 0
- 【无限背包】背包问题
- 背包问题---01背包
- 背包问题--部分背包
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- 背包问题
- unity的2D中物体在Scene窗口中可以看到,但在Game窗口中看不到
- PIL IOError: cannot open resource处理
- javascript 数组方法
- iReport 报表展示报错:Error filling print... Error evaluating expression
- GPU渲染流程学习_02_阴影
- 背包问题
- POI 动态生成ECXEL
- JQuery 自定义事件的使用
- Idea+TestNg配置test-output输出(转)
- ora-29280,ora-06512
- NOIP复赛复习(十二)数论算法巩固与提高
- Laravel 提示字段不存在groupBy
- Qt Linux版本卸载
- malloc函数介绍