Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "string.h"
- #include "array.h"
- using namespace std;
- int mod7(int x)
- {
- return (x % 7 + 7) % 7;
- }
- int polyValueMod7(const Array<int>& coeffs, int x)
- {
- // coeffs[0] -- коэф. при x^n, coeffs[1] -- при x^(n-1), ...
- int n = coeffs.size() - 1; // степень
- int result = 0;
- for (int i = 0; i <= n; i++)
- {
- // степень x^(n - i)
- int exp = n - i;
- int powX = 1;
- for (int j = 0; j < exp; j++)
- powX = mod7(powX * x);
- result = mod7(result + mod7(coeffs[i] * powX));
- }
- return mod7(result);
- }
- int main()
- {
- cout << "Введите степень многочлена: ";
- int deg;
- cin >> deg;
- Array<int> coefficients;
- // Считываем deg+1 коэффициент
- for (int i = 0; i <= deg; i++)
- {
- cout << "coeff[" << i << "]=";
- int c; cin >> c;
- coefficients.append(mod7(c));
- }
- // Ищем корни
- String roots; // для вывода
- bool anyRoot = false;
- for (int x = 0; x < 7; x++)
- {
- int val = polyValueMod7(coefficients, x);
- if (val == 0)
- {
- if (anyRoot) roots = roots + ", ";
- roots = roots + to_string(x).c_str();
- anyRoot = true;
- }
- }
- if (!anyRoot)
- {
- cout << "Нет корней в F7\n";
- }
- else
- {
- cout << "Корни: " << roots << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement