C++编程问题(编译出错)-2
// GreedKnapsack.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream.h>
#include <string.h>
//using namespace std;
void input()
{
static int c;
cout<<"输入背包容量:";
cin>>c;
cout<<endl;
static int n;
cout<<"输入物品数量: ";
cin>>n;
cout<<endl;
float *w=new float[n];
float *v=new float[n];
float *x=new float[n];
static float value;
for(int j=1;j<=n;j++)
{
float a,b;
cout<<"输入第"<<j<<"种物品重量:";
cin>>a;
w[j]=a;
cout<<"价值(大于0):";
cin>>b;
v[j]=b;
x[j]=v[j]/w[j];
cout<<"x:"<<x[j]<<endl;
}
}
static void Greedy()
{
int cu=c;
int i;
int temp=0;
for(i=0;i<n;i++){
temp=sortResult[i];
if(w[temp]>cu){
break;
}
x[temp]=1;
cu-=w[temp];
value=value=value+v[temp];
}
if(i<=n)
{
x[temp]=cu/w[temp];
value=value+x[temp]*v[temp];
}
return;
}
static void sortPerValue()
{
int i,j,index=0,k=0;
float temp;
for(i=0;i<n;i++)
sortResult[i]=0;
for(i=0;i<n;i++)
{
temp=x[i];
index=i;
for(j=0;j<=n;j++)
{
if((temp<x[j])&&(x[j]!=0))
index=j;
}
sortResult[k++]=index;
x[index]=0;
}
return;
}
static void output()
{
int i;
cout<<"可能方案之一为:"<<endl;
cout<<"总价值="<<value<<endl;
for(i=0;i<n;i++)
cout<<x[i]<<" "<<endl;
return;
}
void main(int argc, char* argv[])
{
input();
Greedy();
output();
}
Compiling...
GreedKnapsack.cpp
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(39) : error C2065: 'c' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(42) : error C2065: 'n' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(43) : error C2065: 'sortResult' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(43) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(44) : error C2065: 'w' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(44) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(47) : error C2065: 'x' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(47) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(47) : error C2106: '=' : left operand must be l-value
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(48) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(49) : error C2065: 'value' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(49) : error C2065: 'v' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(49) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(53) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(53) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(53) : error C2106: '=' : left operand must be l-value
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(54) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(54) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(64) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(64) : error C2106: '=' : left operand must be l-value
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(67) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(67) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(71) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(71) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(74) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(74) : error C2106: '=' : left operand must be l-value
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(75) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(75) : error C2106: '=' : left operand must be l-value
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(86) : error C2109: subscript requires array or pointer type
执行 cl.exe 时出错.
GreedKnapsack.obj - 1 error(s), 0 warning(s)