Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <ctime>
- #include <omp.h>
- #include <vector>
- using namespace std;
- string firstStr, secondStr, thirdStr, getPermutation;
- int firstInt, secondInt, thirdInt, choose, k;
- int licz = 0;
- vector<string> readOut;
- void sequency()
- {
- string s("0123456789");
- vector<string> tab;
- cout << endl << "Rozpoczeto generowanie permutacji... ";
- clock_t start = clock();
- do {
- tab.push_back(s);
- } while (next_permutation(s.begin(), s.end()));
- cout << " OK " << endl;
- for (int k = 0; k < tab.size(); k++) {
- getPermutation = tab[k];
- for (int leftSide = 1; leftSide < getPermutation.length() - 1; leftSide++) {
- for (int rightSide = (getPermutation.length() - 1); rightSide > leftSide; rightSide--) {
- firstStr = getPermutation.substr(0, leftSide);
- firstInt = stoi(firstStr);
- secondStr = getPermutation.substr(leftSide, (rightSide - leftSide));
- secondInt = stoi(secondStr);
- thirdStr = getPermutation.substr(rightSide, (getPermutation.length() -rightSide));
- thirdInt = stoi(thirdStr);
- if (secondInt != 0 && (firstInt%secondInt == 0) && ((firstInt / secondInt) + thirdInt == 100))
- {
- readOut.push_back(firstStr + " / " + secondStr + " + " + thirdStr + " = 100");
- licz++;
- }
- if (thirdInt != 0 && (secondInt%thirdInt == 0) && (firstInt + (secondInt / thirdInt) == 100))
- {
- readOut.push_back(firstStr + " + " + secondStr + " / " + thirdStr + " = 100");
- licz++;
- }
- }
- }
- }
- clock_t stop = clock();
- cout << "Wygenerowano liczbe mozliwosci w liczbie: " << tab.size() << endl;
- cout << "Liczba znalezionych liczbe sposobow uzyskania wyniku 100: " << licz << endl;
- cout << "Czas realizacji: " << (stop - start) << endl;
- }
- void parallel()
- {
- string s("0123456789");
- vector<string> tab;
- cout << endl << "Rozpoczeto generowanie permutacji... ";
- double start_time = omp_get_wtime();
- do {
- tab.push_back(s);
- } while (next_permutation(s.begin(), s.end()));
- cout << " OK " << endl;
- #pragma omp parallel for shared(k, tab)
- for (k = 0; k < tab.size(); k++) {
- getPermutation = tab[k];
- for (int leftSide = 1; leftSide < getPermutation.length() - 1; leftSide++) {
- for (int rightSide = (getPermutation.length() - 1); rightSide > leftSide; rightSide--) {
- firstStr = getPermutation.substr(0, leftSide);
- #pragma omp critical
- firstInt = stoi(firstStr, nullptr, 10);
- secondStr = getPermutation.substr(leftSide, (rightSide - leftSide));
- #pragma omp critical
- secondInt = stoi(secondStr, nullptr, 10);
- thirdStr = getPermutation.substr(rightSide, (getPermutation.length() - rightSide));
- #pragma omp critical
- thirdInt = stoi(thirdStr, nullptr, 10);
- if (secondInt != 0 && (firstInt%secondInt == 0) && ((firstInt / secondInt) + thirdInt == 100))
- {
- readOut.push_back(firstStr + " / " + secondStr + " + " + thirdStr + " = 100");
- licz++;
- }
- if (thirdInt != 0 && (secondInt%thirdInt == 0) && (firstInt + (secondInt / thirdInt) == 100))
- {
- readOut.push_back(firstStr + " + " + secondStr + " / " + thirdStr + " = 100");
- licz++;
- }
- }
- }
- }
- double time = omp_get_wtime() - start_time;
- cout << "Wygenerowano liczbe mozliwosci w liczbie: " << tab.size() << endl;
- cout << "Liczba znalezionych liczbe sposobow uzyskania wyniku 100: " << licz << endl;
- cout << "Czas realizacji: " << time << endl;
- }
- void menu()
- {
- system("cls");
- 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;
- cout << "MENU - wybierz interesujaca Cie opcje" << endl;
- cout << "1. Wyszukiwanie liczb w sposob sekwencyjny" << endl;
- cout << "2. Wyszukiwanie liczb w sposob rownoległy" << endl;
- cout << "Twoj wybor (zatwierdz ENTER`em): ";
- cin >> choose;
- switch (choose)
- {
- case 1:
- sequency();
- case 2:
- parallel();
- default:
- cout << "Nie ma takiej opcji w MENU!" << endl;
- menu();
- }
- }
- int main()
- {
- omp_set_num_threads(4);
- menu();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement