要把这些物品装入到容量为100的一批箱子(序号1-N)中。装箱方法是:对每项物品, 顺序扫描箱子,把该物品放入足以能够容下它的第一个箱子中。请写一个程序模拟这种装箱过程,并输出每个物品所在的箱子序号,以及放置全部物品所需的箱子数目。
输入样例:
8
60 70 80 90 30 40 10 20
输出样例:
60 1
70 2
80 3
90 4
30 1
40 5
10 1
20 2
5
#include<iostream>
using namespace std;
#define max 10000
int box[max];
int goods[max];
int s[max];
int main(){
int n;
int count;
count=0;
cin>>n;
for(int i=1;i<=n;i++){
box[i]=100;
cin>>goods[i];
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(goods[i]<=box[j]){
box[j]=box[j]-goods[i];
s[i]=j;
if(count<j)
count=j;
break;
}
}
}
for(int i=1;i<=n;i++)
cout<<goods[i]<<" "<<s[i]<<endl;
cout<<count;
return 0;
}