#include<queue> #include <iostream> using namespace std; #define NUM 100 int n; int c; int w[NUM]; int MaxLoading() { queue<int >Q; Q.push(-1); int i=0; int Ew=0; int bestw=0; int r=0; for(int j=1;j<n;j++) r+=w[j]; while(true) { int wt=Ew+w[i]; if(wt<=c) { if(wt>bestw)bestw=wt; if(i<n-1)Q.push(wt); } if(Ew+r>bestw&&i<n-1)Q.push(Ew); Ew=Q.front(); Q.pop(); if(Ew==-1) { if(Q.empty())return bestw; Q.push(-1); Ew=Q.front(); Q.pop(); i++; r-=w[i]; } } return bestw; } int main() { cout << "请输入物品数量 n 和背包容量 c: "; int n, c; cin >> n >> c; for (int i = 0; i < n; i++) { cout << "请输入第 " << i+1 << " 件物品的重量 :"; cin >> w[i]; } int maxLoading = MaxLoading(); cout << "最大载重量为: " << maxLoading << endl; return 0; }补充代码

时间: 2024-03-22 17:39:23 浏览: 22
这段代码是一个背包问题的解决方案,主要使用了贪心算法和队列实现了最优解的求解。 补充的代码如下: ```c++ #include <iostream> #include <queue> using namespace std; #define NUM 100 int n; int c; int w[NUM]; int MaxLoading() { queue<int> Q; Q.push(-1); int i=0; int Ew=0; int bestw=0; int r=0; for(int j=1;j<n;j++) r+=w[j]; while(true) { int wt=Ew+w[i]; if(wt<=c) { if(wt>bestw)bestw=wt; if(i<n-1)Q.push(wt); } if(Ew+r>bestw&&i<n-1)Q.push(Ew); Ew=Q.front(); Q.pop(); if(Ew==-1) { if(Q.empty())return bestw; Q.push(-1); Ew=Q.front(); Q.pop(); i++; r-=w[i]; } } return bestw; } int main() { cout << "请输入物品数量 n 和背包容量 c: "; cin >> n >> c; for (int i = 0; i < n; i++) { cout << "请输入第 " << i+1 << " 件物品的重量 :"; cin >> w[i]; } int maxLoading = MaxLoading(); cout << "最大载重量为: " << maxLoading << endl; return 0; } ```

相关推荐

#include <stdio.h> #include <stdlib.h> #include <string.h> #include #include <sys/time.h> #include <unistd.h> #include #include <signal.h> #include #include <algorithm> #include <iostream> #include <map> #include <string> #include <queue> #include <vector> #include <sstream> #define LOG_BRASERO_NUM 15 using namespace std; static char *g_cpBrasero[] = { (char *) "ID", (char *) "刻录时间", (char *) "刻录机型号", (char *) "光盘属性", (char *) "刻录状态", (char *) "计算机帐户", (char *) "文件名称", (char *) "文件大小", (char *) "文件类型", (char *) "测试1", (char *) "测试2", (char *) "测试3", (char *) "测试4", (char *) "测试5", (char *) "测试6", }; typedef struct _tagBraseroLog { char cpValue[1024]; } BRASEROLOG; int uosaarch_line_parse(char *pBuffer) { int index, len,lastLen; int ret = 0; char *begin = NULL; char *end = NULL; char *lastEnd = NULL; //debug printf("进入了扫描"); BRASEROLOG BraseroLog[LOG_BRASERO_NUM]; memset(&BraseroLog, 0, LOG_BRASERO_NUM * sizeof(BRASEROLOG)); for (index = 0; index < LOG_BRASERO_NUM; index++) { begin = strstr(pBuffer, g_cpBrasero[index]); if(NULL == begin) continue; begin=strstr(begin,"="); end = strstr(pBuffer, g_cpBrasero[index + 1]); //end--; if (begin != NULL) { len = strlen("="); unsigned long strSize = end - begin - len ; printf("BraseroLOg[%d]=%s\n",index,BraseroLog[index].cpValue); //strncpy(BraseroLog[index].cpValue, begin + len, std::min(strSize, sizeof(BraseroLog[index].cpValue) - 1)); // printf("PrintLog[%d] = %s\n",index,BraseroLog[index].cpValue); } return 0; } return 1; } int main(){ char a[500] = "ID=1689309873, 刻录时间=2023-07-14 12:44:34, 刻录机型号=TSSTcorp-CDDVDW-SE-218CB-R95M6YMDA00008, 光盘属性=DVD+R, 刻录状态=成功, 计算机帐户=hba, 文件名称=/home/hba/Desktop/刻录测试文件.txt, 文件大小=66 B, 文件类型=文档"; uosaarch_line_parse(a); return 0; }

#include<iostream> #include<queue> using namespace std; #define MAXNUM 100 char visited1[MAXNUM]; typedef struct{ char vexs[MAXNUM]; //顶点 int arcs[MAXNUM][MAXNUM];//边 int vexnum,arcnum; } AMGraph; int LocateVex(AMGraph G,char v){ for(int i = 0; i < G.vexnum; i++){ if(G.vexs[i] == v)return i; } return -1; } int CreateUNG(AMGraph &G){ char v1,v2; cout<<"请输入顶点数和边数:"; cin>>G.vexnum>>G.arcnum; cout<<"请依次输入顶点:"; for(int i = 0; i < G.vexnum; i++)cin>>G.vexs[i]; for(int j = 0; j < G.vexnum; j++) for(int i = 0; i < G.vexnum; i++) G.arcs[j][i] = 0; //初始化邻接矩阵 cout<<"请依次输入邻边:"<<endl; for(int k = 0; k < G.arcnum; k++){ cin>>v1>>v2; int i = LocateVex(G,v1); int j = LocateVex(G,v2); G.arcs[i][j] = 1; G.arcs[j][i] = 1; } return 1; } void DFT_AM(AMGraph G,int i){ //深度优先遍历邻接矩阵 cout<<G.vexs[i]; visited1[i] = 1; for(int j = 0; j < G.vexnum; j++){ if(G.arcs[i][j] == 1 && !visited1[j])DFT_AM(G,j); } } void BFT_AM(AMGraph G, int i) { //广度优先遍历邻接矩阵 queue<int> Q; //定义队列Q Q.push(i); //将起始顶点入队 visited1[i] = 1; //标记为已访问 while (!Q.empty()) { //重复步骤2-3,直到队列为空 int cur = Q.front(); //取出队首元素 Q.pop(); //出队 cout << G.vexs[cur]; //访问该顶点 for (int j = 0; j < G.vexnum; j++) { if (G.arcs[cur][j] == 1 && !visited1[j]) { //遍历该顶点的邻接点,将未访问的邻接点入队 Q.push(j); visited1[j] = 1; //标记为已访问 } } } } int main(){ AMGraph G; CreateUNG(G); for(int j = 0; j < G.vexnum; j++){ //输出邻接矩阵 for(int i = 0; i < G.vexnum; i++) cout<<G.arcs[j][i]<<" "; cout<<endl; } cout<<endl<<"输出深度优先序列:"; DFT_AM(G,0); cout << endl << "输出广度优先序列:"; for (int i = 0; i < MAXNUM; i++) visited1[i] = 0; //重置visited1数组 BFT_AM(G, 0); } 请改良此代码

#include <iostream> #include <queue> using namespace std; // 定义图的最大顶点数 #define MAX_VERTEX_NUM 100 // 图的邻接矩阵表示 typedef struct { int vertex[MAX_VERTEX_NUM]; // 顶点数组 int edges[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵 int vertexNum; // 顶点数量 int edgeNum; // 边数量 } GraphMatrix; // 初始化图 void initGraph(GraphMatrix& graph) { graph.vertexNum = 0; graph.edgeNum = 0; for (int i = 0; i < MAX_VERTEX_NUM; i++) { graph.vertex[i] = 0; for (int j = 0; j < MAX_VERTEX_NUM; j++) { graph.edges[i][j] = 0; } } } // 添加顶点 void addVertex(GraphMatrix& graph, int v) { if (graph.vertexNum < MAX_VERTEX_NUM) { graph.vertex[graph.vertexNum] = v; graph.vertexNum++; } } // 添加边 void addEdge(GraphMatrix& graph, int v1, int v2) { int i, j; for (i = 0; i < graph.vertexNum; i++) { if (graph.vertex[i] == v1) break; } for (j = 0; j < graph.vertexNum; j++) { if (graph.vertex[j] == v2) break; } if (i < graph.vertexNum && j < graph.vertexNum) { graph.edges[i][j] = 1; graph.edges[j][i] = 1; graph.edgeNum++; } } // 销毁图 void destroyGraph(GraphMatrix& graph) { graph.vertexNum = 0; graph.edgeNum = 0; } // 深度优先遍历 void depthFirstSearch(GraphMatrix& graph, int v, bool visited[]) { visited[v] = true; cout << graph.vertex[v] << " "; for (int i = 0; i < graph.vertexNum; i++) { if (graph.edges[v][i] == 1 && !visited[i]) { depthFirstSearch(graph, i, visited); } } } // 深度优先遍历图 void DFS(GraphMatrix& graph) { bool visited[MAX_VERTEX_NUM]; for (int i = 0; i < graph.vertexNum; i++) { visited[i] = false; } for (int i = 0; i < graph.vertexNum; i++) { if (!visited[i]) { depthFirstSearch(graph, i, visited); } } cout << endl; } // 广度优先遍历图 void BFS(GraphMatrix& graph) { bool vis这段代码的不足

最新推荐

recommend-type

grpcio-1.63.0-cp38-cp38-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

SQLyog-13.1.3-0.x86Community.exe

SQLyog-13.1.3-0.x86Community
recommend-type

VB自动出题题库系统设计(源代码+系统).rar

计算机专业毕业设计VB精品论文资源
recommend-type

debugpy-1.0.0b2-cp35-cp35m-manylinux1_i686.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

实战自学python如何成为大佬(目录):https://blog.csdn.net/weixin-67859959/artic

实战自学python如何成为大佬(目录):https://blog.csdn.net/weixin-67859959/artic
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。