- 假设有N项物品,大小分别为
要把这些物品装入到容量为100的一批箱子(序号1-N)中。装箱方法是:对每项物品, 顺序扫描箱子,把该物品放入足以能够容下它的第一个箱子中。请写一个程序模拟这种装箱过程,并输出每个物品所在的箱子序号,以及放置全部物品所需的箱子数目。
输入格式:
- 输入第一行给出物品个数N(≤1000);第二行给出N个正整数
输出格式:
- 按照输入顺序输出每个物品的大小及其所在的箱子序号,每个物品占1行,最后一行输出所需的箱子数目。
输入样例:
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
结尾无空行
AC:
#include <bits/stdc++.h>
using namespace std;
int main()
{
std::ios_base::sync_with_stdio(false);
int n, max = 1;
cin >> n;
int *s = new int[n];
int *a = new int[n];
for (int i = 1; i <= n; i++)
cin >> s[i];
for (int i = 1; i <= n; i++)
a[i] = 100;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (s[i] <= a[j])
{
if (j > max)
max = j;
cout << s[i] << " " << j << endl;
a[j] = a[j] - s[i];
break;
}
}
}
cout << max << endl;
delete (s);
return 0;
}
WA:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
std::ios_base::sync_with_stdio(false);
int n, max = 1;
cin >> n;
int *s = new int[n];
int *a = new int[n];
for (int i = 1; i <= n; i++)
cin >> s[i];
for (int i = 1; i <= n; i++)
a[i] = 100;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (s[i] <= a[j])
{
if (j > max)
max = j;
cout << s[i] << " " << j << endl;
a[j] = a[j] - s[i];
break;
}
}
}
cout << max << endl;
delete (s);
delete (a);
return 0;
}