#include <stdio.h>
#include <iostream>
using namespace std;
float allc=0;
int score_back(float b[100],int A[100][2],int n,int c[100])//找出标记为0的单位价值最大的序号
{
float max1=0;
int jmax;
for(int j=1;j<=n;j++)
{
b[j]=float(A[j][0])/float(A[j][1]);//单位价值
//cout<<b[j]<<endl;
if(b[j]>max1&&c[j]!=1)
{
max1=b[j];
jmax=j;
}
}
return jmax;
}
int main()
{
float b[100];
int n,m,counts=0,num;
int A[100][2];
int c[100],T[100];
int x,temp;
cout<<"please cout n and m"<<endl;
cin>>n>>m;//输入物品数和限重
for(int i=1;i<=n;i++)
{
cout<<"please cout "<<i<<" value and weight"<<endl;
cin>>A[i][0]>>A[i][1];//输入每个物品的价值和重量
c[i]=0;//标志,如果为0,代表没找过
}
temp=score_back(b,A,n,c);
while(m-A[temp][1]>=0)//限重只要可以减去
{
allc
贪心算法之分数背包问题
最新推荐文章于 2024-05-23 23:22:37 发布