Advertisement
Night_H4nter

Untitled

Feb 13th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <bits/stdc++.h>
  4. #include <string>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. vector<int> twoSum(vector<int> nums, int target)
  10. {
  11. vector<int> result;
  12.  
  13. for (unsigned long int i1 = 0; i1 < nums.size()-1; ++i1)
  14. {
  15. for (unsigned long int i2 = i1+1; i2 < nums.size(); ++i2 )
  16. {
  17. if (nums[i1] + nums[i2] == target)
  18. {
  19. result.push_back(i1);
  20. result.push_back(i2);
  21.  
  22. }
  23.  
  24. if (result.empty())
  25. result = {0};
  26. return result;
  27. }
  28. }
  29. }
  30.  
  31. vector<int> splitBySpace(string str)
  32. {
  33. vector<string> sresult;
  34. vector<int> result;
  35. string word = "";
  36.  
  37. for (auto x : str)
  38. {
  39. if (x == ' ')
  40. {
  41. sresult.push_back(word);
  42. word = "";
  43. }
  44. else
  45. {
  46. word = word + x;
  47. }
  48. }
  49.  
  50. for (string a : sresult)
  51. {
  52. result.push_back(stoi(a));
  53. }
  54. return result;
  55. }
  56.  
  57. int main()
  58. {
  59. vector<int> inputnums;
  60. int target;
  61. string input;
  62.  
  63. cout << "Enter your integer numbers array: " << endl;
  64. cin >> input;
  65. cout << "Enter desired target: " << endl;
  66. cin >> target;
  67.  
  68. vector<int> tmp = splitBySpace(input);
  69. vector<int> finalv = twoSum(tmp, target);
  70. for (int i : finalv)
  71. cout << i << endl;
  72.  
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement