C ++程序实现对一维对象和M箱的首次拟合递减
这是一个C++程序,用于对一维对象和M个容器实施首次拟合递减
所需功能和伪代码:
Begin function binPack() returns number of bins required. Initialize binC = 0 Initialize an array to store binVal. Place items one by one. function sort() to perform bubble sort in the descending order. End
范例程式码
#include <iostream>
using namespace std;
void binPack(int *a, int s, int n)
{
int binC = 0;
int binVal[n];
for (int i = 0; i < n; i++)
binVal[i] = s;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (binVal[j] - a[i] >= 0)
{
binVal[j] -= a[i];
break;
}
}
for (int i = 0; i < n; i++)
if (binVal[i] != s)
binC++;
cout << "Number of bins required using first fit decreasing algorithm
is: "
<< binC;
}
int* sort(int *seq, int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n - 1; j++)
if (seq[j] < seq[j + 1])
{
seq[j] = seq[j] + seq[j + 1];
seq[j + 1] = seq[j] - seq[j + 1];
seq[j] = seq[j] - seq[j + 1];
}
return seq;
}
int main(int argc, char **argv)
{
cout << "Enter the number of items in Set: ";
int n;
cin >> n;
cout << "Enter " << n << " items:";
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
cout << "Enter the bin size: ";
int s;
cin >> s;
int *seq = sort(a, n);
binPack(seq, s, n);
}输出结果
Enter the number of items in Set: 7 Enter 7 items:4 6 7 5 3 2 1 Enter the bin size: 5 Number of bins required using first fit decreasing algorithm is: 3
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志