能不能帮看一下,这个程序我是从网上下载的,作用大概就是分解因式,能不能告诉我怎么在这里面输入而不是直接用它定义的变量
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
// ¶¨Òå¶àÏîʽÊý¾Ý½á¹¹
struct Polynomial {
vector<int> coefficients; // °´½µÃÝÅÅÁд洢¶àÏîʽϵÊý
// »ñÈ¡¶àÏîʽµÄ½×Êý
int degree() const {
return coefficients.size() - 1;
}
// Êä³ö¶àÏîʽ
void print() const {
for (int i = 0; i < coefficients.size(); ++i) {
int coeff = coefficients[i];
int power = coefficients.size() - 1 - i;
if (coeff != 0) {
if (i > 0 && coeff > 0) cout << "+";
if (coeff != 1 || power == 0) cout << coeff;
if (power > 0) cout << "x";
if (power > 1) cout << "^" << power;
}
}
cout << endl;
}
};
// շתÏà³ý·¨¼ÆËãÁ½¸öÕûÊýµÄ×î´ó¹«ÒòÊý
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return abs(a);
}
// ¶àÏîʽϵÊýµÄ×î´ó¹«ÒòÊý
int poly_gcd(const Polynomial &poly) {
int result = abs(poly.coefficients[0]);
for (int coeff : poly.coefficients) {
result = gcd(result, coeff);
}
return result;
}
// ÌáÈ¡¹«Òòʽ
Polynomial extract_common_factor(Polynomial &poly) {
int factor = poly_gcd(poly);
if (factor > 1) {
for (int &coeff : poly.coefficients) {
coeff /= factor;
}
}
return Polynomial{{factor}};
}
// ³¢ÊÔÕÒµ½Ò»¸öÕûϵÊýµÄ¸ù
int find_root(const Polynomial &poly) {
int constant_term = abs(poly.coefficients.back());
for (int i = 1; i <= sqrt(constant_term); ++i) {
if (constant_term % i == 0) {
// ¼ì²éÕý¸º¸ù
for (int root : {i, -i}) {
int value = 0;
for (int j = 0; j < poly.coefficients.size(); ++j) {
value = value * root + poly.coefficients[j];
}
if (value == 0) return root;
}
}
}
return 0; // δÕÒµ½¸ù
}
// ¶àÏîʽ³ý·¨£¨ÓÃÓÚÒòʽ·Ö½â£©
Polynomial divide(const Polynomial &poly, int root) {
vector<int> new_coeffs;
int remainder = 0;
for (int coeff : poly.coefficients) {
remainder = remainder * root + coeff;
new_coeffs.push_back(remainder);
}
new_coeffs.pop_back(); // ÒÆ³ý×îºóµÄÓàÊý
return Polynomial{new_coeffs};
}
// Òòʽ·Ö½â
vector<Polynomial> factorize(Polynomial poly) {
vector<Polynomial> factors;
// ÌáÈ¡¹«Òòʽ
Polynomial common_factor = extract_common_factor(poly);
factors.push_back(common_factor);
// ·Ö½âÊ£Óಿ·Ö
while (poly.degree() > 1) {
int root = find_root(poly);
if (root == 0) break; // ÎÞ·¨ÕÒµ½ÕûϵÊý¸ù
// ¹¹ÔìÒòʽ (x - root)
Polynomial factor{{-root, 1}};
factors.push_back(factor);
// ¶àÏîʽ³ý·¨
poly = divide(poly, root);
}
if (poly.degree() > 0) {
factors.push_back(poly);
}
return factors;
}
int main() {
Polynomial poly{{1, -6, 11, -6}}; // x^3 - 6x^2 + 11x - 6
cout << "Ôʼ¶àÏîʽ: ";
poly.print();
vector<Polynomial> factors = factorize(poly);
cout << "Òòʽ·Ö½â½á¹û: ";
for (const auto &factor : factors) {
factor.print();
cout << " ";
}
cout << endl;
return 0;
}