Advertisement
_Mazur

C++/2014MPR

Mar 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <algorithm>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <map>
  6. #include <numeric>
  7. #include <string>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. constexpr bool IsPrime(unsigned long value) {
  13.  
  14.     if(value == 1) {
  15.         return false;
  16.     }
  17.  
  18.     for(unsigned long div = 2; div*div <= value; ++div) {
  19.         if(value % div == 0) {
  20.             return false;
  21.         }
  22.     }
  23.  
  24.     return true;
  25.  
  26. }
  27.  
  28. class Program {
  29.  
  30. public:
  31.  
  32.     Program() {
  33.  
  34.         ifstream file("NAPIS.txt");
  35.         copy(istream_iterator<string>{file}, istream_iterator<string>{}, back_inserter(words));
  36.  
  37.     }
  38.  
  39.     void Run() {
  40.  
  41.         Task1();
  42.         Task2();
  43.         Task3();
  44.  
  45.     }
  46.  
  47. private:
  48.  
  49.     void Task1() {
  50.  
  51.         cout << "1) " << count_if(words.begin(), words.end(), [](const string& word) {
  52.             return IsPrime(accumulate(word.begin(), word.end(), 0UL));
  53.         });
  54.         cout << '\n';
  55.  
  56.     }
  57.  
  58.     void Task2() {
  59.  
  60.         cout << "2) ";
  61.         copy_if(words.begin(), words.end(), ostream_iterator<string>{cout, "\n"}, [](const string& word) {
  62.             return is_sorted(word.begin(), word.end());
  63.         });
  64.  
  65.     }
  66.  
  67.     void Task3() {
  68.  
  69.         map<string, size_t> wordCounter;
  70.  
  71.         for(auto&& word : words) {
  72.             ++wordCounter[word];
  73.         }
  74.  
  75.         cout << "3)\n";
  76.         for(auto[word, count] : wordCounter) {
  77.             if(count > 1) {
  78.                 cout << word << '\n';
  79.             }
  80.         }
  81.  
  82.     }
  83.  
  84.     vector<string> words;
  85.  
  86. };
  87.  
  88. int main() {
  89.    
  90.     Program program;
  91.     program.Run();
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement