博主原创,未经允许,不得转载!
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<math.h>;
#define n 6
void ark(float A[][6], float x1[], float y1[],int n1) {
for (int r = 0; r <= n1 - 1; r++) {
A[r][0] = y1[r];
}
for (int k = 1; k <= n1; k++) {
for (int r = k; r <= n1; r++) {
A[r][k] = (A[r][k - 1] - A[r - 1][k - 1]) / (x1[r] - x1[r - k]);
}
}
for (int i = 0; i <= n1; i++) {
printf("%f ", A[i][i]);
}
}
//此算法运用递归可以算出组合数
float C(int r, int k, float x[], int n1) {
float s = 0;
if (k == 0)
return 1;
else if (k == 1) {
for (int i = 0; i < r; i++)
s = s + x[i];
return s;
}
else {
for (int j = k - 1; j<r; j++)
s = s + x[j] * C(j, k - 1, x, n1);
return s;
}
}
void main() {
/*
printf("请输入个数n", "\n");
scanf("%d", &n);
printf("请输入相应的坐标", "\n");
for (int i = 0; i <= n - 1; i++) {
printf("x[%d]", i);
scanf("%f", &x[i]);
printf("y[%d]", i);
scanf("%f", &y[i]);
printf("\n");
}
*/
float A[n][n], B[n];
float x[n] = { 0.40,0.55,0.65,0.80,0.90,1.05 };
float y[n] = { 0.41075,0.57815,0.69675,0.88811,1.02652,1.25382 };
ark(A,x,y,6);
for (int k = n-1; k >=0; k--) {
B[k] = 0;
for (int i = 0; i <n - k; i++) {
B[k] = B[k] + pow(-1, i)*(A[k+i][k+i])*C(k + i, i, x, n);
}
}
for (int i = 0; i <n; i++) {
printf("%f ", B[i]);
}
system("pause");
}