牛頓法求多項式的根
阿新 • • 發佈:2018-12-03
C++程式碼
#include <iostream> #include <cstdio> using namespace std; const int maxn = 105; int a[maxn]; int n, t; int main() { cin >> n >> t; for (int i = 0; i <= n; i++) cin >> a[i]; cout << "原式:" ; for (int i = 0; i <= n; i++) { if (i) cout << " + "; cout << a[i] ; if (i != n) cout << "x^" << n-i; cout << " "; } cout << endl; int r, s; r = a[0]; a[0] = 0; for (int i = 1; i <= n + 1; i++) { s = a[i]; a[i] = r; r = r * t + s; } //for (int i = 0; i < 5; i++) cout << a[i] << " "; //cout << endl; cout << "變形:" ; printf("(x - %d) * ", t); cout << "( "; for (int i = 1; i <= n; i++) { if (i != 1) cout << " + "; cout << a[i] ; if (i != n ) cout << "x^" << n - i; } cout << ") + " << a[n + 1] << endl; return 0; }
測試資料
/*
2 4
1 2 -24
2 4
1 2 -25
3 2
1 -2 1 4
*/