问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
#include <iostream>
using namespace std;
double newTon(double s, double x[], double y[], int n)
{
double p = y[0], g, f;
for (int i = 0; i < n; i++)
{
for (int j = n - 1; j > i; j--)
{
y[j] = (y[j] - y[j - 1]) / (x[j] - x[j - i - 1]); //动态规划
}
g = y[i + 1];
for (int k = 0; k <= i; k++)
g *= (s - x[k]);
p += g;
}
return p;
}
int main()
{
double y1[3] = {0.36789441, 0.135335283, 0.049787068};
double x1[3] = {1, 2, 3};
cout << newTon(2.6, x1, y1, 3) << endl;
double x2[3] = {1, 2, 3};
double y2[3] = {0.36789441, 0.135335283, 0.049787068};
cout << newTon(2.6, x2, y2, 3) << endl;
return 0;
}
运行结果及报错内容
0.066365
-0.317635
我的解答思路和尝试过的方法
在devc和vs里出来的两个答案是一样的,但在vc里是不同的