Advertisement
Guest User

9

a guest
May 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <algorithm>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8. ////////////////////////////
  9. const int N = 7;
  10. int test_size[N]={0};
  11. int sum = 0;
  12. int record = 0;
  13. int price1 = 0;
  14. int index = 0;
  15. int max_s = 30;
  16.  
  17.  
  18. void forw(float v[] , float c[]){
  19.     for(int i = index; i < N; i++){
  20.         if(v[i] + sum <= max_s){
  21.             test_size[i] = 1;
  22.             sum+=v[i];
  23.             //price1 += c[i];
  24.         }
  25.         else test_size[i] = 0;
  26.     }
  27.     //if(record < price1)
  28.         //record = price1;
  29. }
  30.  
  31. void check(float v[] , float c[]){
  32.     sum = 0;
  33.     for(int i = 0; i < N; i++){
  34.         sum+=v[i]*test_size[i];
  35.         price1+=c[i]*test_size[i];
  36.     }
  37.     if(sum > max_s || price1 < record)
  38.         return;
  39.     else record = price1;
  40.     price1 = 0;
  41. }
  42.  
  43. int backing(float v[] , float c[]){
  44.     if(test_size[N-1]){
  45.         test_size[N-1] = 0;
  46.         sum-=v[N-1];
  47.     }
  48.     for(int i = N-2; i >=0; --i){
  49.         if(test_size[i]){
  50.             test_size[i] = 0;
  51.             sum-=v[i];
  52.             index = i + 1;
  53.             return 1;
  54.         }
  55.         return 0;
  56.     }
  57. }
  58.  
  59. void outF(float a[]) {
  60.     printf("\n{ ");
  61. for(int i=0; i<N; ++i) {
  62.     if(i<N-1)
  63.         printf("%2.2f, ", a[i]);
  64.     else
  65.         printf("%2.2f }\n", a[i]);
  66. }
  67. }
  68.  
  69. int main()
  70. {
  71.     //cout << "INPUT COUNT THINGS" << endl;
  72.     //cin >> N; /// вводим число вещей
  73.     //cout << "INPUT SIZE OF BACKPACK" << endl;
  74.     //cin >> max_s;
  75.     float v[N] = {7 , 10 , 15 , 25 , 30 , 20 , 15}; /// массив веса вещи
  76.     float c[N] = {49 , 60 , 120 , 50 , 90 , 40 , 150};/// массив стоимости вещи
  77.     float u[N]; /// массив удельной стоимости 1 вещи (индексы сохраняются по порядку)
  78.     for(int i=0; i<N; ++i) {
  79.         u[i] = (float)(c[i]/v[i]);
  80.    }
  81.    float arr[N] = {0};
  82.     for(int i=0; i<N; i++)
  83.         for(int j=1; j<N-1; j++)
  84.             if(u[j] > u[i]) {
  85.                 int t = u[j];
  86.                 u[j] = u[i];
  87.                 u[i] = t;
  88.  
  89.                  t = c[j];
  90.                 c[j] = c[i];
  91.                 c[i] = t;
  92.  
  93.                  t = v[j];
  94.                 v[j] = v[i];
  95.                 v[i] = t;
  96.             }
  97.  
  98.     cout << "things:";
  99.     outF(v);
  100.     cout << "prices:";
  101.     outF(c);
  102.     cout << "unit cost:";
  103.     outF(u);
  104.  
  105.     do {
  106.         forw(v , c);
  107.         check(v , c);
  108.     } while(backing(v , c));
  109.     printf("\nRESULT OF ALGORITHM 3rd: %d", record);
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement