您的位置:首页 > 其它

第五章作业第一部分

2014-04-18 08:41 281 查看
例5.1

# include<iostream>
using namespace std;
void display()
{
cout<<"this is an example."<<endl;
}
int main()
{
display();
return 0;
}

习题1

# include<iostream>
# include<cmath>
using namespace std;
double squ(double x);
int main()
{
double x;
cout<<"please input x:";
cin>>x;
cout<<"the square root of"<<x<<"is"<<squ(x)<<endl;
return 0;
}
double squ(double x)
{
double s1,s2;
s1=0.5*(1.0+x);
do
{
s2=s1;
s1=(s2+x/s2)*0.5;
}while(fabs(s2-s1)>1.0E-6);
return s1;
}

例5.2

# include<iostream>
using namespace std;
double min(double x,double y)
{
return x<y?x:y;
}
int main()
{
cout<<min(6.0,5.0)<<endl;
return 0;
}


例5.5

# include<iostream>
using namespace std;
int ncomp(int i,int j)
{
if(i>j) return 1;
if (i==j) return 0;
return -1;
}
int main()
{
int k=2;
int n=ncomp(k,++k);
cout<<n;
return 0;
}

例5.7

# include<iostream>
using namespace std;
int sqr(int x)
{
x=x*x;
return x;
}
int main()
{
int t=10;
int s=sqr(t);
cout<<"t="<<t<<'\t'
<<"sqr("<<t<<")="<<s<<endl;
return 0;
}

例5.10

# include<iostream>
using namespace std;
void display(int x,float y)
{
cout<<x<<""<<y;
return;
}
int main()
{
float a;
int b;
cin>>b>>a;
display(b,a);
return 0;
}


例5.13

# include<iostream>
using namespace std;
long f2(int);
long f1(int p)
{
int k;
long r;
k=p*p;
r=f2(k);
return r;
}
long f2(int q)
{
long fact=1;
for(int i=1;i<=q;i++)
fact*=i;
return fact;
}
int main()
{
int i;
long sum=0;
for(i=2;i<=3;i++)
sum+=f1(i);
cout<<"sum="<<sum<<endl;
return 0;
}

例5.16

# include<iostream>
using namespace std;
const N=8;
long fibo(int n);
int main()
{
long f=fibo(N);
cout<<"Fibonacci数列第8项的值为:"<<f<<endl;
return 0;
}
long fibo(int n)
{
if(n==1) return 1L;
else if(n==2) return 1L;
else
return fibo(n-1)+fibo(n-2);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: