Advertisement
HabKaffee

Untitled

Oct 31st, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <fstream>
  5.  
  6. std::ifstream FileIn("Task.txt");
  7.  
  8.  
  9. double fullSearch(int i, double* arrayOfWeight, double* arrayOfCost, double curentWeight, int totalMaxWeight) {
  10.     if (i < 0) {
  11.         return 0;
  12.     }
  13.     double temp = 0;
  14.     if (arrayOfWeight[i] + curentWeight <= totalMaxWeight) {
  15.         temp = fullSearch(i - 1, arrayOfWeight, arrayOfCost, arrayOfWeight[i] + curentWeight, totalMaxWeight) + arrayOfCost[i];
  16.     }
  17.     return std::max(temp, fullSearch(i - 1, arrayOfWeight, arrayOfCost, curentWeight, totalMaxWeight));
  18. }
  19.  
  20.  
  21. int main() {
  22.  
  23.     int totalMaxWeight = 0, numberOfItem = 0;
  24.  
  25.     FileIn >> numberOfItem >> totalMaxWeight;
  26.  
  27.     double* arrayOfWeight = new double[numberOfItem];
  28.     double* arrayOfCost = new double[numberOfItem];
  29.  
  30.     for (size_t i = 0; i < numberOfItem; ++i) {
  31.         FileIn >> arrayOfWeight[i];
  32.     }
  33.     for (size_t i = 0; i < numberOfItem; ++i) {
  34.         FileIn >> arrayOfCost[i];
  35.     }
  36.     double resOfFullSearch =  fullSearch(numberOfItem - 1, arrayOfWeight, arrayOfCost, 0, totalMaxWeight);
  37.     std::cout <<"Result of full search: "<< resOfFullSearch<<std::endl;
  38.  
  39.  
  40.  
  41.     delete[] arrayOfWeight;
  42.     delete[] arrayOfCost;
  43.    
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement