Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. #include <iostream> // подключение библиотеки
  2. #include <chrono>
  3.  
  4. class Timer //класс для времени
  5. {
  6. public:
  7.     Timer() // конструктор
  8.         : start_(std::chrono::high_resolution_clock::now())
  9.     {
  10.     }
  11.  
  12.     ~Timer() // деструктор с выводом времени
  13.     {
  14.         const auto finish = std::chrono::high_resolution_clock::now();
  15.         std::cout << std::chrono::duration_cast<std::chrono::microseconds>(finish - start_).count() << " us" << std::endl;
  16.     }
  17.  
  18. private:
  19.     const std::chrono::high_resolution_clock::time_point start_;
  20. };
  21.  
  22. void Pi(int n, int** pi) // объявление функции
  23. {
  24.     Timer t; // инициализация таймера
  25.     int length = 10 * n / 3 + 1, cnt_9 = 0, count_oper = 0; // объявление переменных
  26.     // length необходимая длина временного массива
  27.     // cnt_9 счетчик 9-ок
  28.     int dgt = 0, *data, j;
  29.     data = new int[length]; // создание временного массива
  30.     for (int i = 0; i < length; i++) // заполнение массива 2-ками
  31.         data[i] = 2;
  32.  
  33.     for (j = 0; j < n; j++) // запуск цикла, зависящего от необходимого количества цифр
  34.     {
  35.         int tmp = 0;
  36.         for (int i = length; i > 0; i--) // запуск цикла в обратную сторону
  37.         {
  38.             int x  = 10 * data[i-1] + tmp*i; // формулы
  39.             count_oper += 2;
  40.             data[i-1] = x % (2*i - 1);
  41.             count_oper += 3;
  42.             tmp = x / (2*i - 1);
  43.             count_oper += 3;
  44.         }
  45.         data[0] = tmp % 10;
  46.         tmp /= 10;
  47.         count_oper += 2;
  48.  
  49.         if (tmp == 9) cnt_9++; // увеличение счетчика
  50.         else if (tmp == 10) // если цифра оказалась равной 10
  51.         {
  52.             *(*(pi)+j) = dgt + 1; // вывод цифры на 1 больше
  53.             for (int i = 0; i < cnt_9; i++) // выводим 0
  54.                 *(*(pi)+j) = 0; // *(*(pi)+j) ~ pi[j] необходимо, так как массив передан по ссылке
  55.             dgt = 0;
  56.             cnt_9 = 0;
  57.         }
  58.         else
  59.         {
  60.             //вывод очередной цифры
  61.             *(*(pi)+j) = dgt;
  62.             dgt = tmp;
  63.             if (cnt_9 != 0) // вывод 9-ок
  64.                 for (int i = 0; i < cnt_9; i++)
  65.                     *(*(pi)+j) = 9;
  66.             cnt_9 = 0;
  67.         }
  68.         if (j == 10 || j == 100 || j == 1000 || j == 10000) // вывод значений
  69.         {
  70.             std::cout << "N = " << n << " j = " << j << " operations = " << count_oper << " time = ";
  71.             t.~Timer();
  72.         }
  73.     }
  74.     *(*(pi)+j) = dgt; // вывод последней цифры
  75.     std::cout << "Total time: ";
  76. }
  77.  
  78. int main()
  79. {
  80.     int N = 1000, *pi;
  81.     pi = new int[N+1]; // создание массива для хранения цифр
  82.     Pi(N, &pi); // запуск функции
  83.     std::cout << "pi = ";
  84.     for (int i = 1; i <= N; i++)
  85.     {
  86.         std::cout << pi[i];
  87.         if (i == 1) std::cout << ","; // для красивого вывода
  88.     }
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement