Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  Polindrom
  4. //
  5. //  Created by Никита Сергеев on 26.07.16.
  6. //  Copyright © 2016 Никита Сергеев. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <string>
  11. #include <vector>
  12. #include <algorithm>
  13. #include <iterator>
  14. #include <map>
  15. #include <fstream>
  16. #include <stdexcept>
  17.  
  18. #define limit_of_iterations 20 // сюда вводи ограничение по количеству итераций для каждого числа.
  19. #define directory "/Users/nikitasergeev/Desktop/result1.txt"
  20.  
  21. bool check_polyndrom(std::string &str) {
  22.    
  23.     unsigned long long middle_of_number = str.size() / 2;
  24.    
  25.     for (unsigned long long i = 0, j = str.size() - 1; i < middle_of_number && j > (middle_of_number - 1); i++, j--) {
  26.         if (str[i] == str[j]) {
  27.             continue;
  28.         } else {
  29.             return false;
  30.         }
  31.     }
  32.     return true;
  33. }
  34.  
  35. unsigned long long reverse_number(std::string &str) {
  36.    
  37.     std::reverse(std::begin(str),
  38.                  std::end(str));
  39.     unsigned long long num;
  40.  
  41.     try {
  42.         num = std::stoi(str);
  43.     } catch (std::out_of_range& e) {
  44.         //std::cerr << e.what();
  45.     }
  46.    
  47.     return num;
  48. }
  49.  
  50. unsigned long long counter(std::string &str, int &iterations) {
  51.     unsigned long long buf = 0;
  52.     unsigned long long numb1, numb2;
  53.     numb1 = std::stoi(str);
  54.    
  55.     while (!check_polyndrom(str) && iterations < limit_of_iterations) {
  56.         numb2 = reverse_number(str);
  57.         numb1 += numb2;
  58.         str = std::to_string(numb1);
  59.         iterations++;
  60.     }
  61.     return buf;
  62. }
  63.  
  64.  
  65. int main(int argc, const char * argv[]) {
  66.  
  67.     std::map<unsigned long long, std::string> number_and_iterations;
  68.     unsigned long long numeric = 10, changed_numeric = 0;
  69.     while (numeric < 100000) {
  70.         int iterations = 0;
  71.        
  72.         std::string input_str = std::to_string(numeric);
  73.        
  74.         if (!check_polyndrom(input_str)) {
  75.             changed_numeric = counter(input_str, iterations);
  76.         } else {
  77.             number_and_iterations.insert(std::pair<unsigned long, std::string>(numeric, std::to_string(iterations)));
  78.         }
  79.        
  80.         if (iterations == limit_of_iterations && !check_polyndrom(input_str))
  81.             number_and_iterations.insert(std::pair<unsigned long long, std::string>(numeric, "а хуй посчитаешь."));
  82.         else
  83.             number_and_iterations.insert(std::pair<unsigned long long, std::string>(numeric, std::to_string(iterations)));
  84.  
  85.         numeric++;
  86.     }
  87.    
  88.     std::ofstream fout(directory);
  89.     if (fout.is_open()) {
  90.         std::map<unsigned long long, std::string>::iterator it;
  91.         for (it=number_and_iterations.begin(); it!=number_and_iterations.end(); ++it) {
  92.             //std::cout << it->first << " => " << it->second << '\n';
  93.             fout << it->first << " => " << it->second << '\n';
  94.         }
  95.     } else {
  96.         std::cerr << "АЛОЭЭЭ" << std::endl;
  97.     }
  98.    
  99.  
  100.    
  101.    
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement