实验十
10.1
1 #include<stdio.h> 2 3 long f(long n); 4 5 int main() 6 { 7 long n; 8 9 scanf("%ld",&n); 10 11 printf("%ld\n",f(n)); 12 13 return 0; 14 } 15 16 long f(long n) 17 { 18 if(n>2) 19 return f(n-1)+f(n-2); 20 else 21 return 1; 22 }
10.2
1 #include<stdio.h> 2 3 int sum(int n); 4 5 int main() 6 { 7 printf("%d\n",sum(100)); 8 9 return 0; 10 } 11 12 int sum(int n) 13 { 14 if(n>1) 15 return n+sum(n-1); 16 else 17 return 1; 18 }
10.3
1 #include<stdio.h> 2 #include<math.h> 3 4 int main() 5 { 6 double x,a; 7 8 scanf("%lf",&a); 9 10 x=pow(a,1.0/3); 11 12 while(a/(3.0*x*x)-1.0/3*x<=-0.00001||a/(3.0*x*x)-1.0/3*x>=0.00001) 13 { 14 x=a/(3.0*x*x)+2.0/3*x; 15 } 16 17 printf("x=%lf\n",x); 18 19 return 0; 20 }
10.4
1 #include<stdio.h> 2 3 double p(int n,double x); 4 5 int main() 6 { 7 int n; 8 double x; 9 10 scanf("%lf%d",&x,&n); 11 12 printf("%f\n",p(n,x)); 13 14 return 0; 15 } 16 17 double p(int n,double x) 18 { 19 if(n>1) 20 return ((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))*1.0/n; 21 else if(n==1) 22 return x; 23 else 24 return 1; 25 }
10.5
1 #include<stdio.h> 2 #include<math.h> 3 4 int main() 5 { 6 double x0=2.0,x1=2.1,x,b; 7 8 b=(x1-log(x1)-2)/(x1-x0+log(x0)-log(x1))*(x0-x1); 9 //log(x)相当于数学里ln(X) 10 while(b>=0.00001||b<=-0.00001) 11 { 12 x=x1; 13 x1=x1+b; 14 x0=x; 15 b=(x1-log(x1)-2)/(x1-x0+log(x0)-log(x1))*(x0-x1); 16 } 17 18 printf("x=%lf\n",x1); 19 20 return 0; 21 }