nguyenhappy92

Cái túi (Xếp ba lô)

Nov 1st, 2015
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. // xay dung code giai bai toan xep ba lo hay bai toan cai tui
  2. // Khai bao cac ham thu vien neu co
  3. #include<iostream>
  4. #include<fstream>
  5. #include<iomanip>
  6. using namespace std;
  7. // Xay dung struct nhu sau
  8. struct MONDO
  9. {
  10. unsigned w; //trong luong món do
  11. int v; //giá tri món do
  12. unsigned n; //so luong món do
  13. };
  14. //Khai báo bien toàn cuc
  15. MONDO a[100];
  16. int n;//so loai món do khác nhau
  17. //doc du lieu tu tap tin file
  18. void ifp(char *fname, MONDO a[], int &n)
  19. {
  20. ifstream fp;
  21. fp.open("nhap.txt");
  22. if (!fp)exit(1);
  23. else
  24. {
  25. int i;
  26. fp >> n;
  27. for (i = 0; i < n; i++)
  28. fp >> a[i].w;
  29. for (i = 0; i < n; i++)
  30. fp >> a[i].v;
  31. for (i = 0; i < n; i++)
  32. fp >> a[i].n;
  33. }
  34. }
  35. //
  36. void swp(MONDO &a, MONDO &b)
  37. {
  38. MONDO tmp = a;
  39. a = b;
  40. b = tmp;
  41. }
  42. // sap xep mon do giam dan theo don gia
  43. void sort(MONDO a[], int n)
  44. {
  45. for (int i = 0; i<n - 1; i++)
  46. for (int j = i + 1; j<n; j++)
  47. if (a[i].v*1.0 / a[i].w < a[j].v*1.0 / a[j].w)
  48. swp(a[i], a[j]);
  49. }
  50. //sap xep mon do giam dan theo gia tri
  51. void sort2(MONDO a[], int n)
  52. {
  53. for (int i = 0; i<n - 1; i++)
  54. for (int j = i + 1; j<n; j++)
  55. if (a[i].v < a[j].v)
  56. swp(a[i], a[j]);
  57. }
  58. // Xep mon do tang dan theo trong luong
  59. void sort3(MONDO a[], int n)
  60. {
  61. for (int i = 0; i<n - 1; i++)
  62. for (int j = i + 1; j<n; j++)
  63. if (a[i].w > a[j].w)
  64. swp(a[i], a[j]);
  65. }
  66. // su dung thuat toan tham lam de giai bai nay
  67. int greedy(MONDO a[], int n,MONDO chon[], int &nc,unsigned c)
  68. {
  69.  
  70. sort2(a, n);
  71. int tong = 0, i;
  72. nc = 0;
  73. do {
  74. i = 0;
  75.  
  76. while (i < n && a[i].w>c || a[i].n == 0)
  77. i++;
  78. if (i < n)
  79. {
  80. chon[nc++] = a[i];
  81. c -= a[i].w;
  82. tong += a[i].v;
  83. a[i].n--;
  84. }
  85. } while (i < n);
  86. return tong;
  87. }
  88. //xuat so luong
  89. void opn(MONDO a[], int n)
  90. {
  91. cout << "number: ";
  92. for (int i = 0; i < n; i++)
  93. cout << setw(3) << a[i].n;
  94. cout << endl;
  95. }
  96. //xuat trong luong
  97. void opw(MONDO a[], int n)
  98. {
  99. cout << "weight: ";
  100. for (int i = 0; i < n; i++)
  101. cout << setw(3) << a[i].w;
  102. cout << endl;
  103. }
  104. // Xuat gia tri
  105. void opv(MONDO a[], int n)
  106. {
  107. cout << "value : ";
  108. for (int i = 0; i < n; i++)
  109. cout << setw(3) << a[i].v;
  110. cout << endl;
  111. }
  112. // Xuat ket qua
  113. void inkq(MONDO c[], int n)
  114. {
  115. int i;
  116. for (i = 0; i<n; i++)
  117. {
  118. cout << "[" << setw(2) << c[i].w << "," << setw(2) << c[i].v <<
  119. "]" << endl;
  120. }
  121. }
  122. // Chuong trinh chinh
  123. int main()
  124. {
  125. MONDO chon[100];
  126. int nc, max;
  127. ifp("ip.txt", a, n);
  128. opw(a, n);
  129. opv(a, n);
  130. opn(a, n);
  131. max = greedy(a, n, chon, nc, 37);
  132. cout << endl << "Danh sach cac mon hang:" << endl;
  133. inkq(chon, nc);
  134. cout << "Tong cac gia tri mon hang lay duoc: " << max << endl;
  135. return 0;
  136. }
  137. // tao 1 file .txt nhu sau
Advertisement
Add Comment
Please, Sign In to add comment