I have a task and solution. How can I solve it with the help of dynamical programming or recurrent descent?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
void minPoly(const string &str, double x) {
vector<double> coefficients;
if (x <= 1.0) {
for (int i = 0; i < str.size(); i++) {
int a = str[i] - '0';
cout << a << " ";
coefficients.push_back(a);
}
}
else {
double L = 0, R = stod(str), tenf = pow(10.0, str.size() - 1.0), xn = 1.0;
for (int i = 0; i < str.size(); i++) {
int a = str[i] - '0';
cout << a;
L = 10.0*L+a;
R -= tenf*a*xn;
if (( tenf - 1.0 )*L > R/xn(x-1.0)) {
coefficients.push_back(L);
L = 0.0;
R *= x;
xn *= x;
cout << " ";
}
tenf /= 10.0;
}
coefficients.push_back(L);
}
cout << '\n';
double poly = 0.0, xn = 1.0;
for (double c : coefficients) {
poly += c*xn;
xn *= x;
}
cout << poly << '\n';
}
int main() {
string str;
double x;
cout << "Enter string: "; cin >> str;
cout << "Enter x: "; cin >> x;
minPoly(str, x);
}
|
Enter string: 1234
Enter x: 1
1 2 3 4
10
|
Last edited on