最优装载
来源:互联网 发布:java gc原理 编辑:程序博客网 时间:2024/04/29 22:27
#include "stdio.h"#include "stdlib.h"#include "string.h"#define MAX 20 typedef struct Item{ double w; //集装箱重量 int index; //集装箱下标}Item;Item item[MAX]; //集装箱double x[MAX]; //x[i]表示是否装入,装入为1,不装入为0int cmp(const void *item1, const void *item2) //对集装箱按重量升序{ if((*(Item *)item1).w > (*(Item *)item2).w) return 1; else return 0;}//返回轮船所装集装箱最大个数//n为集装箱总数//c为轮船可装总重量int Loading(int n, double c){ qsort(item, n, sizeof(Item), cmp); //将集装箱按重量升序 memset(x, 0, sizeof(x)); //初始化解向量空间 int i; int count = 0; for(i=0; i<n && item[i].w<c; i++) //如果集装箱可以装船 { x[i] = 1; //装入 c -= item[i].w; //可装重量减少 count++; } return count;}int main(){ int n = 4; double c = 70; double w[] = {20, 10, 26, 15}; int i; for(i=0; i<n; i++) { item[i].w = w[i]; item[i].index = i+1; } printf("轮船载重为:%.2f\n", c); printf("现有的集装箱为:\n"); for(i=0; i<n; i++) { printf("集装箱%d(%.2f)\n", item[i].index, item[i].w); } printf("\n\n"); int u = Loading(n, c); printf("轮船所装集装箱的个数最大为: %d\n", u); printf("所选的集装箱为:\n"); for(i=0; i<n; i++) { if(x[i]) printf("集装箱%d(%.2f)\n", item[i].index, item[i].w); } printf("\n"); return 0;}
0 0
- 最优装载
- 最优装载
- 最优装载
- 最优装载
- 最优装载
- 最优装载(贪心)
- 贪心-最优装载问题
- 最优装载问题
- 最优装载问题
- 最优装载方案
- 贪心,最优装载问题
- 贪心算法-最优装载
- 最优装载问题
- 最优装载(贪心算法)
- 背包问题,最优装载问题
- 最优装载问题 回溯算法
- 贪心入门--最优装载问题
- 最优装载问题(贪心)
- oracle 命令1
- pgadmin & phppgadmin的配置
- 继承 构造器和析构器
- 嵌入式软件开发过程--编译、链接和定址
- POJ 1.11编程基础之二分查找
- 最优装载
- mongodb数据文件格式(一)
- vitesse X use llvm speed up postgresql
- 快速熟悉项目代码
- golang笔记
- matlab中find函数的使用说明
- CMD命令之Echo
- CADisplayLink 及定时器的使用
- MySQL的BLOB类型