Advertisement
Toliak

20180924_1

Sep 24th, 2018
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. #define ABS(x) ((x) < 0 ? (-(x)) : (x))
  5.  
  6. // Цикл
  7. int main() {
  8.     setlocale(LC_ALL, "Russian");
  9.  
  10.     // Расчет суммый от 1 до N элемента
  11.     {
  12.         unsigned int n;
  13.         std::cout << "Введите N: ";
  14.         std::cin >> n;
  15.  
  16.         double s = 0;
  17.         for (unsigned int i = 1; i <= n; i++) {
  18.             int factorial = 1;
  19.             for (int f = 2; f <= i; f++) {
  20.                 factorial *= f;
  21.             }
  22.             double value = ((i % 2 == 0) ? 1. : -1.) / (double)factorial;
  23.             s += value;
  24.         }
  25.  
  26.         std::cout << "Сумма: " << s << std::endl;
  27.     }
  28.  
  29.     // Расчет бесконечной суммы с точностью
  30.     {
  31.         double t;
  32.         std::cout << "Введите точность: ";
  33.         std::cin >> t;
  34.  
  35.         double s = 0;
  36.         double value;
  37.         unsigned i = 0;
  38.         do {
  39.             i++;
  40.             int factorial = 1;
  41.             for (int f = 2; f <= i; f++) {
  42.                 factorial *= f;
  43.             }
  44.             value = ((i % 2 == 0) ? 1. : -1.) / (double)factorial;
  45.             s += value;
  46.         } while (ABS(value) >= ABS(t));
  47.  
  48.         std::cout << "Сумма: " << s << std::endl;
  49.     }
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement