50166. Newton's Method
難度:3.3/5
微積分好難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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int d;
double coff[16] = {0};
double diff[16] = {0};
double f(double x){
double p = 1;
double ans = (double)0;
for(int i = 0; i <= d; i++){
ans += p * coff[i];
p *= x;
}
return ans;
}
double df(double x){
double p = 1;
double ans = (double)0;
for(int i = 0; i < d; i++){
ans += p * diff[i];
p *= x;
}
return ans;
}
int main(){
scanf("%d", &d);
int dummy = d;
while(dummy >= 0) scanf("%lf", &coff[dummy--]);
int k; double x;
scanf("%d", &k);
scanf("%lf", &x);
for(int i = 0; i < d; i++) diff[i] = (double)(i + 1) * coff[i + 1];
for(int i = 0; i < k; i++){
double y = f(x);
printf("%.4f %.4f\n", x, y);
double m = df(x);
x -= (y / m);
}
}