Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Polindrom
- //
- // Created by Никита Сергеев on 26.07.16.
- // Copyright © 2016 Никита Сергеев. All rights reserved.
- //
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <iterator>
- #include <map>
- #include <fstream>
- #include <stdexcept>
- #define limit_of_iterations 20 // сюда вводи ограничение по количеству итераций для каждого числа.
- #define directory "/Users/nikitasergeev/Desktop/result1.txt"
- bool check_polyndrom(std::string &str) {
- unsigned long long middle_of_number = str.size() / 2;
- for (unsigned long long i = 0, j = str.size() - 1; i < middle_of_number && j > (middle_of_number - 1); i++, j--) {
- if (str[i] == str[j]) {
- continue;
- } else {
- return false;
- }
- }
- return true;
- }
- unsigned long long reverse_number(std::string &str) {
- std::reverse(std::begin(str),
- std::end(str));
- unsigned long long num;
- try {
- num = std::stoi(str);
- } catch (std::out_of_range& e) {
- //std::cerr << e.what();
- }
- return num;
- }
- unsigned long long counter(std::string &str, int &iterations) {
- unsigned long long buf = 0;
- unsigned long long numb1, numb2;
- numb1 = std::stoi(str);
- while (!check_polyndrom(str) && iterations < limit_of_iterations) {
- numb2 = reverse_number(str);
- numb1 += numb2;
- str = std::to_string(numb1);
- iterations++;
- }
- return buf;
- }
- int main(int argc, const char * argv[]) {
- std::map<unsigned long long, std::string> number_and_iterations;
- unsigned long long numeric = 10, changed_numeric = 0;
- while (numeric < 100000) {
- int iterations = 0;
- std::string input_str = std::to_string(numeric);
- if (!check_polyndrom(input_str)) {
- changed_numeric = counter(input_str, iterations);
- } else {
- number_and_iterations.insert(std::pair<unsigned long, std::string>(numeric, std::to_string(iterations)));
- }
- if (iterations == limit_of_iterations && !check_polyndrom(input_str))
- number_and_iterations.insert(std::pair<unsigned long long, std::string>(numeric, "а хуй посчитаешь."));
- else
- number_and_iterations.insert(std::pair<unsigned long long, std::string>(numeric, std::to_string(iterations)));
- numeric++;
- }
- std::ofstream fout(directory);
- if (fout.is_open()) {
- std::map<unsigned long long, std::string>::iterator it;
- for (it=number_and_iterations.begin(); it!=number_and_iterations.end(); ++it) {
- //std::cout << it->first << " => " << it->second << '\n';
- fout << it->first << " => " << it->second << '\n';
- }
- } else {
- std::cerr << "АЛОЭЭЭ" << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement