Advertisement
Guest User

OR

a guest
Jan 21st, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <omp.h>
  7. #include <vector>
  8.  
  9.  
  10. using namespace std;
  11. string firstStr, secondStr, thirdStr, getPermutation;
  12. int firstInt, secondInt, thirdInt, choose, k;
  13. int licz = 0;
  14. vector<string> readOut;
  15.  
  16. void sequency()
  17. {
  18. string s("0123456789");
  19. vector<string> tab;
  20.  
  21. cout << endl << "Rozpoczeto generowanie permutacji... ";
  22.  
  23. clock_t start = clock();
  24.  
  25. do {
  26. tab.push_back(s);
  27. } while (next_permutation(s.begin(), s.end()));
  28.  
  29. cout << " OK " << endl;
  30.  
  31. for (int k = 0; k < tab.size(); k++) {
  32. getPermutation = tab[k];
  33. for (int leftSide = 1; leftSide < getPermutation.length() - 1; leftSide++) {
  34. for (int rightSide = (getPermutation.length() - 1); rightSide > leftSide; rightSide--) {
  35. firstStr = getPermutation.substr(0, leftSide);
  36. firstInt = stoi(firstStr);
  37.  
  38. secondStr = getPermutation.substr(leftSide, (rightSide - leftSide));
  39. secondInt = stoi(secondStr);
  40.  
  41. thirdStr = getPermutation.substr(rightSide, (getPermutation.length() -rightSide));
  42. thirdInt = stoi(thirdStr);
  43.  
  44. if (secondInt != 0 && (firstInt%secondInt == 0) && ((firstInt / secondInt) + thirdInt == 100))
  45. {
  46. readOut.push_back(firstStr + " / " + secondStr + " + " + thirdStr + " = 100");
  47. licz++;
  48. }
  49. if (thirdInt != 0 && (secondInt%thirdInt == 0) && (firstInt + (secondInt / thirdInt) == 100))
  50. {
  51. readOut.push_back(firstStr + " + " + secondStr + " / " + thirdStr + " = 100");
  52. licz++;
  53. }
  54. }
  55. }
  56. }
  57.  
  58. clock_t stop = clock();
  59. cout << "Wygenerowano liczbe mozliwosci w liczbie: " << tab.size() << endl;
  60. cout << "Liczba znalezionych liczbe sposobow uzyskania wyniku 100: " << licz << endl;
  61. cout << "Czas realizacji: " << (stop - start) << endl;
  62. }
  63.  
  64. void parallel()
  65. {
  66. string s("0123456789");
  67. vector<string> tab;
  68.  
  69. cout << endl << "Rozpoczeto generowanie permutacji... ";
  70.  
  71. double start_time = omp_get_wtime();
  72.  
  73. do {
  74. tab.push_back(s);
  75. } while (next_permutation(s.begin(), s.end()));
  76.  
  77. cout << " OK " << endl;
  78.  
  79. #pragma omp parallel for shared(k, tab)
  80. for (k = 0; k < tab.size(); k++) {
  81. getPermutation = tab[k];
  82. for (int leftSide = 1; leftSide < getPermutation.length() - 1; leftSide++) {
  83. for (int rightSide = (getPermutation.length() - 1); rightSide > leftSide; rightSide--) {
  84. firstStr = getPermutation.substr(0, leftSide);
  85. #pragma omp critical
  86. firstInt = stoi(firstStr, nullptr, 10);
  87.  
  88. secondStr = getPermutation.substr(leftSide, (rightSide - leftSide));
  89. #pragma omp critical
  90. secondInt = stoi(secondStr, nullptr, 10);
  91.  
  92. thirdStr = getPermutation.substr(rightSide, (getPermutation.length() - rightSide));
  93. #pragma omp critical
  94. thirdInt = stoi(thirdStr, nullptr, 10);
  95.  
  96. if (secondInt != 0 && (firstInt%secondInt == 0) && ((firstInt / secondInt) + thirdInt == 100))
  97. {
  98. readOut.push_back(firstStr + " / " + secondStr + " + " + thirdStr + " = 100");
  99. licz++;
  100. }
  101. if (thirdInt != 0 && (secondInt%thirdInt == 0) && (firstInt + (secondInt / thirdInt) == 100))
  102. {
  103. readOut.push_back(firstStr + " + " + secondStr + " / " + thirdStr + " = 100");
  104. licz++;
  105. }
  106. }
  107. }
  108. }
  109.  
  110. double time = omp_get_wtime() - start_time;
  111. cout << "Wygenerowano liczbe mozliwosci w liczbie: " << tab.size() << endl;
  112. cout << "Liczba znalezionych liczbe sposobow uzyskania wyniku 100: " << licz << endl;
  113. cout << "Czas realizacji: " << time << endl;
  114. }
  115.  
  116. void menu()
  117. {
  118. system("cls");
  119. cout << "Program znajdujacy liczbe sposobow w jaki mozna wstawic operacje + i / pomiedzy cyfry 0, 1, . . ., 9 w taki sposob, aby powstale dzialanie dalo wynik rowny 100" << endl;
  120. cout << "MENU - wybierz interesujaca Cie opcje" << endl;
  121. cout << "1. Wyszukiwanie liczb w sposob sekwencyjny" << endl;
  122. cout << "2. Wyszukiwanie liczb w sposob rownoległy" << endl;
  123. cout << "Twoj wybor (zatwierdz ENTER`em): ";
  124. cin >> choose;
  125.  
  126. switch (choose)
  127. {
  128. case 1:
  129. sequency();
  130. case 2:
  131. parallel();
  132. default:
  133. cout << "Nie ma takiej opcji w MENU!" << endl;
  134. menu();
  135. }
  136. }
  137.  
  138.  
  139. int main()
  140. {
  141. omp_set_num_threads(4);
  142. menu();
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement