Advertisement
Solingen

z3.3.cpp

Dec 21st, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include "string.h"
  3. #include "array.h"
  4. using namespace std;
  5.  
  6. int mod7(int x)
  7. {
  8.     return (x % 7 + 7) % 7;
  9. }
  10.  
  11. int polyValueMod7(const Array<int>& coeffs, int x)
  12. {
  13.     // coeffs[0] -- коэф. при x^n, coeffs[1] -- при x^(n-1), ...
  14.     int n = coeffs.size() - 1; // степень
  15.     int result = 0;
  16.     for (int i = 0; i <= n; i++)
  17.     {
  18.         // степень x^(n - i)
  19.         int exp = n - i;
  20.         int powX = 1;
  21.         for (int j = 0; j < exp; j++)
  22.             powX = mod7(powX * x);
  23.  
  24.         result = mod7(result + mod7(coeffs[i] * powX));
  25.     }
  26.     return mod7(result);
  27. }
  28.  
  29. int main()
  30. {
  31.     cout << "Введите степень многочлена: ";
  32.     int deg;
  33.     cin >> deg;
  34.  
  35.     Array<int> coefficients;
  36.     // Считываем deg+1 коэффициент
  37.     for (int i = 0; i <= deg; i++)
  38.     {
  39.         cout << "coeff[" << i << "]=";
  40.         int c; cin >> c;
  41.         coefficients.append(mod7(c));
  42.     }
  43.  
  44.     // Ищем корни
  45.     String roots; // для вывода
  46.     bool anyRoot = false;
  47.     for (int x = 0; x < 7; x++)
  48.     {
  49.         int val = polyValueMod7(coefficients, x);
  50.         if (val == 0)
  51.         {
  52.             if (anyRoot) roots = roots + ", ";
  53.             roots = roots + to_string(x).c_str();
  54.             anyRoot = true;
  55.         }
  56.     }
  57.  
  58.     if (!anyRoot)
  59.     {
  60.         cout << "Нет корней в F7\n";
  61.     }
  62.     else
  63.     {
  64.         cout << "Корни: " << roots << endl;
  65.     }
  66.  
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement