Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<stack>
  4. #include<math.h>
  5. #include<stdlib.h>
  6. long long currentDiff = -1;
  7. long long current = -1;
  8. long long target;
  9.  
  10. void foo(long long x, long long i, long long initial, std::vector<int> path,std::vector<int> vect)
  11. {
  12. for (int u = i+1; u < vect.size(); ++u)
  13. {
  14. if (u == initial)
  15. {continue;}
  16. int temp = vect.at(u);
  17. std::vector<int> temp2 = vect;
  18. temp2.erase(temp2.begin()+u);
  19. for (int uu = i; uu < temp2.size(); ++uu)
  20. {
  21. if (uu==initial)
  22. {
  23. continue;
  24. }
  25. std::vector<int> temp3 = temp2;
  26. temp3.at(uu) = temp3.at(uu)*temp;
  27. foo(x,i,initial,path,temp3);
  28. }
  29. }
  30.  
  31. if (abs(x-target) <= currentDiff)
  32. {
  33. current = x;
  34. currentDiff = abs(x-target);
  35. //std::cout << "path" << std::endl;
  36. for (int k = 0; k < path.size(); ++k)
  37. {
  38. // std::cout << path.at(k) << std::endl;
  39. }
  40. //std::cout << current << std::endl;
  41. }
  42. if( x > target)
  43. {
  44. return;
  45. }
  46. if (i >= vect.size())
  47. {return;
  48. }
  49. if (i == initial)
  50. {
  51.  
  52. foo(x,i+1,initial,path,vect);
  53. return;
  54. }
  55. foo(x,i+1,initial,path,vect);
  56. path.push_back(vect.at(i));
  57. foo(x+vect.at(i),i+1,initial,path,vect);
  58.  
  59. foo(x*vect.at(i),i+1,initial,path,vect);
  60.  
  61.  
  62. return;
  63.  
  64. }
  65.  
  66. int main()
  67. {
  68. std::cin >> target;
  69. std::vector<int> vect;
  70.  
  71. long long temp;
  72. while(std::cin >> temp)
  73. {
  74. vect.push_back(temp);
  75. }
  76. current = vect.at(0);
  77. currentDiff = abs(vect.at(0)-target);
  78.  
  79. for (long long i = 0; i < vect.size(); ++i)
  80. {
  81. //std::cout << vect.at(i) << std::endl;
  82. std::cout << "was ran" << std::endl;
  83. std::vector<int> path;
  84. path.push_back(vect.at(i));
  85. foo(vect.at(i),0,i,path,vect);
  86. }
  87. std::cout << current << std::endl;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement