Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<stack>
- #include<math.h>
- #include<stdlib.h>
- long long currentDiff = -1;
- long long current = -1;
- long long target;
- void foo(long long x, long long i, long long initial, std::vector<int> path,std::vector<int> vect)
- {
- for (int u = i+1; u < vect.size(); ++u)
- {
- if (u == initial)
- {continue;}
- int temp = vect.at(u);
- std::vector<int> temp2 = vect;
- temp2.erase(temp2.begin()+u);
- for (int uu = i; uu < temp2.size(); ++uu)
- {
- if (uu==initial)
- {
- continue;
- }
- std::vector<int> temp3 = temp2;
- temp3.at(uu) = temp3.at(uu)*temp;
- foo(x,i,initial,path,temp3);
- }
- }
- if (abs(x-target) <= currentDiff)
- {
- current = x;
- currentDiff = abs(x-target);
- //std::cout << "path" << std::endl;
- for (int k = 0; k < path.size(); ++k)
- {
- // std::cout << path.at(k) << std::endl;
- }
- //std::cout << current << std::endl;
- }
- if( x > target)
- {
- return;
- }
- if (i >= vect.size())
- {return;
- }
- if (i == initial)
- {
- foo(x,i+1,initial,path,vect);
- return;
- }
- foo(x,i+1,initial,path,vect);
- path.push_back(vect.at(i));
- foo(x+vect.at(i),i+1,initial,path,vect);
- foo(x*vect.at(i),i+1,initial,path,vect);
- return;
- }
- int main()
- {
- std::cin >> target;
- std::vector<int> vect;
- long long temp;
- while(std::cin >> temp)
- {
- vect.push_back(temp);
- }
- current = vect.at(0);
- currentDiff = abs(vect.at(0)-target);
- for (long long i = 0; i < vect.size(); ++i)
- {
- //std::cout << vect.at(i) << std::endl;
- std::cout << "was ran" << std::endl;
- std::vector<int> path;
- path.push_back(vect.at(i));
- foo(vect.at(i),0,i,path,vect);
- }
- std::cout << current << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement