数据结构:老师自己出的一道题。
2个回答
展开全部
#include<iostream>
#include<cmath>
#include<iomanip>
#define PI 3.14
using namespace std;
struct warehouse
{
int volume;
}warehouses[100];//保存仓库的容量的数据结构, 可以往里面加vector 保存仓库内的物品信息
void init(int n)
{
warehouses[0].volume=0x3fffffff; //下标0处设为极大值,n+1设置为极小值
warehouses[n+1].volume=-1;
for (int i=1;i<=n;i++)
{
warehouses[i].volume=10;
}
}
int putin(int val,int n)//入库操作
{
int minn=0;
for (int i=1;i<=n;i++)
{
if (warehouses[i].volume-val>=0)
if (warehouses[minn].volume>warehouses[i].volume)
minn=i;
}
warehouses[minn].volume-=val;
return minn;
}
int takeout(int val,int n)//出库操作
{
int maxx=n+1;
for (int i=1;i<=n;i++)
{
if (10-warehouses[i].volume-val>=0)
if (warehouses[maxx].volume<warehouses[i].volume)
maxx=i;
}
warehouses[maxx].volume+=val;
return maxx;
}
int main()
{
string str;
int number,n;
cout<<"please enter the number of warehouses (n<=100)"<<endl;
cin>>n;
init(n);
while (cin>>str>>number)
{
if (str[0]=='o')
{
int tmp=takeout(number,n);
cout<<"now the volume of warehouses "<<tmp<<" is "<<warehouses[tmp].volume<<endl;
}
else if (str[0]=='i')
{
int tmp=putin(number,n);
cout<<"now the volume of warehouses "<<tmp<<" is "<<warehouses[tmp].volume<<endl;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询