C语言趣味编程题
不死身兔
1 #include <iostream> 2 using namespace std; 3 int fab(int n) 4 { 5 if(n==1 || n==2) 6 { 7 return 1; 8 } 9 if(n>2) 10 { 11 return fab(n-1)+fab(n-2); 12 } 13 } 14 int main() 15 { 16 for(int i=1;i<=30;i++) 17 { 18 cout<<fab(i)<<endl; 19 } 20 21 }
牛顿迭代法求方程根
1 #include <iostream> 2 #include <iomanip> 3 #include <cmath> 4 using namespace std; 5 int main() 6 { 7 double a,b,c,d; 8 double x,x0=1,f,f1,y; 9 cin>>a>>b>>c>>d; 10 do{ 11 x=x0; 12 f=a*x0*x0*x0+b*x0*x0+c*x0+d; 13 f1=3*a*x0*x0+2*b*x0+c; 14 x0=x0-f/double(f1); 15 }while(fabs(x0-x)>1e-5); 16 cout<<fixed<<setprecision(6)<<x0<<endl; 17 }