贪心算法部分背包问题 c语言,C++贪心算法实现部分背包问题

1 #include

2 #include

3 #include

4 #include

5 #include

6 #include

7 using namespace std;

8 struct object

9 {

10 int no;

11 double weight;

12 double value;

13 double average;

14 };

15 bool cmp(const object &x, const object &y)

16 {

17 return x.average > y.average;//从小到大排

18 }

19 void greedySelector(int m,int W,int solution[],struct object object[]){

20 int i = 0,V = 0,j = 0;

21 while(object[i].weight < W)

22 {

23 solution[i] = 1;

24 W = W - object[i].weight;

25 V = V + object[i].value;

26 i++;

27 }

28 V = V + (W/object[i].weight)*object[i].value;

29 solution[i] = 1;

30 cout << "The corresponding value of the optimal option is:" << V << endl;

31 /*for( i = 0; i < m; i++)

32 {

33 if(solution[i] == 1)

34 {

35 cout << object[i].no << endl;

36 }

37 }*/

38 }

39 int main(void)

40 {

41 LARGE_INTEGER nFreq;

42 LARGE_INTEGER nBeginTime;

43 LARGE_INTEGER nEndTime;

44 ofstream fout1;

45 ofstream fout2;

46 srand((unsigned int)time(NULL));

47 int m,i,j,t;

48 double W;

49 double cost;

50 cout << "Please enter the number of times you want to run the program:";

51 cin >> t;

52 fout1.open("backpack-object.txt",ios::app);

53 if(!fout1){

54 cerr<

55 return -1;

56 }

57 fout1.setf(ios_base::fixed,ios_base::floatfield); //防止输出的数字使用科学计数法

58 fout2.open("backpack-weight.txt",ios::app);

59 if(!fout2){

60 cerr<

61 return -1;

62 }

63 fout2.setf(ios_base::fixed,ios_base::floatfield); //防止输出的数字使用科学计数法

64 for (j = 0;j < t;j++)

65 {

66 cout << "——————————————————The "<< j + 1 << "th test —————————————————"<

67 m = 1 + rand()%100000; //物品个数

68 W = 10 + rand()%100000; //背包总重量

69 fout1 << m << ",";

70 fout2 << (int)W << ",";

71 int solution[m];

72 object object[m];

73 for( i = 0;i < m;i++)

74 {

75 object[i].no = i + 1;

76 object[i].value = 1 + rand()%10000;

77 object[i].weight = 1 + rand()%10000;

78 object[i].average = object[i].value/object[i].weight;

79 }

80 QueryPerformanceFrequency(&nFreq);

81 QueryPerformanceCounter(&nBeginTime);

82 sort(object,object + m,cmp);

83 greedySelector(m,W,solution,object);

84 QueryPerformanceCounter(&nEndTime);

85 cost=(double)(nEndTime.QuadPart - nBeginTime.QuadPart) / (double)nFreq.QuadPart;

86 fout1 << cost << endl;

87 fout2 << cost << endl;

88 cout << "The running time is:" << cost << " s" << endl;

89 }

90 fout1.close();

91 fout2.close();

92 cout << endl;

93 cout << "Success!" << endl;

94 return 0;

95 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,背包问题贪心算法主要有两种,一种是按照物品的价值密度来选择,即每个物品的单位重量价值尽可能大的先选;另一种是按照物品的重量来选择,即每次选择最轻的物品放入背包中。以下是第二种贪心算法C语言实现代码: ```c #include <stdio.h> #define N 100 struct Item { int weight; int value; } items[N]; int cmp(const void* a, const void* b) { const struct Item* x = (const struct Item*)a; const struct Item* y = (const struct Item*)b; double r1 = (double)x->value / x->weight; double r2 = (double)y->value / y->weight; if (r1 < r2) return -1; else if (r1 > r2) return 1; else return 0; } double fractional_knapsack(int W, int n) { int i; double ans = 0.0; qsort(items, n, sizeof(struct Item), cmp); for (i = n - 1; i >= 0 && W > 0; --i) { if (items[i].weight <= W) { ans += items[i].value; W -= items[i].weight; } else { ans += (double)items[i].value * W / items[i].weight; W = 0; } } return ans; } int main() { int W = 50; // 背包总重量 int n = 3; // 物品数量 items[0].weight = 10; items[0].value = 60; items[1].weight = 20; items[1].value = 100; items[2].weight = 30; items[2].value = 120; double ans = fractional_knapsack(W, n); printf("最大价值为 %lf\n", ans); return 0; } ``` 这段代码实现了一个分数背包问题,即物品可以被分割成小块放入背包中。在代码中我们首先以物品的价值密度对物品进行排序,然后按照从大到小的顺序依次选择物品放入背包中,直到背包被放满或所有物品都被放完为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值