第五章作业第一部分
2014-04-18 08:41
281 查看
例5.1
习题1
例5.2
例5.5
例5.7
例5.10
例5.13
例5.16
# 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); }
相关文章推荐
- 第五章作业
- 第五章作业part1
- 第五章作业2
- 第五章作业part2
- 王爽汇编第五章作业 [BX]和LOOP指令
- 第五章:作业
- (36)'c++:COMPLETE REFERENCE' 第一部分 第五章(指针) 第二节
- 第五章作业一(课本习题)
- 第五章作业
- 第五章作业
- 26-《电子入门趣谈》第五章_仿真世界里的电子元器件-第一部分-Mutisim简介
- 【windows核心编程】 第五章 作业
- Java私塾跟我学系列――JAVA篇 第五章作业
- (37)'c++:COMPLETE REFERENCE' 第一部分 第五章(指针) 第三节
- 数据库第五章作业
- 第五章作业
- 高级编程作业 第五章
- 作业6——第五章习题选做
- 第五章作业
- 第五章作业一