1、钱能c+习题答案(二)2.1#include void main() /本题原考虑在16位机器上实验目前多为32位机器,故已过时。 int a = 42486; cout oct a endl hex a endl; unsigned b = 42486; cout dec (signed)b endl;2.2#include #include const double pi = 3.1415926;void main() double radius1, radius2; cout radius1 radius2; cout setw(10) pi setw(10) radius1 setw(
2、10) (pi*radius1*radius1) endl setw(10) pi setw(10) radius2 setw(10) (pi*radius2*radius2) endl;2.3#include #include const double e = 2.718281828;void main() cout setprecision(10) e endl setiosflags(ios:fixed) setprecision(8) e endl setiosflags(ios:scientific) e endl;2.4#include void main() cout How m
3、any students here?n 500n;2.5#include void main() cout size of char sizeof(char) byten size of unsigned char sizeof(unsigned char) byten size of signed char sizeof(signed char) byten size of int sizeof(int) byten size of unsigned sizeof(unsigned) byten size of signed sizeof(signed) byten size of shor
4、t sizeof(short) byten size of unsigned short sizeof(unsigned short) byten size of long sizeof(long) byten size of signed long sizeof(signed long) byten size of unsigned long sizeof(unsigned long) byten size of float sizeof(float) byten size of double sizeof(double) byten size of long double sizeof(l
5、ong double) byten;2.61)please input 3 sides of one triangle:6,6,8a= 6.00,b= 6.00,c= 8.00area of triangle is 17.888542)该程序计算三角形的面积前后分为三部分:输入,处理,输出。3)/#include #include #include #include void main() float a,b,c,s,area; /printf(please input 3 sides of one triangle:n); cout a b c; /输入时以空格作为数据间隔 s=(a+b+c
6、)/2; area=sqrt(s*(s-a)*(s-b)*(s-c); /printf(a=%7.2f,b=%7.2f,c=%7.2fn,a,b,c); cout setiosflags(ios:fixed) setprecision(2) a= setw(7) a ,b= setw(7) b ,c= setw(7) c endl; /printf(area of triangle is %10.5f,area); cout area of triangle is setw(10) setprecision(5) area endl;4)#include #include #include f
7、loat area(float a, float b, float c); /函数声明void main() float a,b,c; cout a b c; /输入时以空格作为数据间隔 float result = area(a,b,c); /函数调用 cout setiosflags(ios:fixed) setprecision(2) a= setw(7) a ,b= setw(7) b ,c= setw(7) c endl; cout area of triangle is setw(10) setprecision(5) result endl;float area(float a,
8、 float b, float c) /函数定义 float s=(a+b+c)/2; return sqrt(s*(s-a)*(s-b)*(s-c);2.7In main():Enter two numbers:3 8Calling add():In add(),received 3 and 8and return 11Back in main():c was set to 11Exiting.2.8#include #include double Cylinder(double r, double h);void main() double radius, height; cout rad
9、ius height; double volume = Cylinder(radius, height); cout 该圆柱体的体积为: volume endl;double Cylinder(double r, double h) return r*r*M_PI*h;(三)3.1(1) sqrt(pow(sin(x),2.5)(2) (a*x+(a+x)/(4*a)/2(3) pow(c,x*x)/sqrt(2*M_PI) /M_PI为BC中math.h中的圆周率常数3.2 13.7 2.5 93.3(1) a1=1 a2=1(2) 1.1(3) 2,0.0(4) 203.4#include
10、 void main() int x; cout x; if(x=-1) cout (x-1) -1 & x=2) cout 2*x endl; if(2x & x=10) cout x*(x+2);3.5#include void main() int a; cout a; int c1 = a%3 =0; int c2 = a%5 =0; int c3 = a%7 =0; switch(c12)+(c21)+c3) case 0: cout 不能被3,5,7整除.n; break; case 1: cout 只能被7整除.n; break; case 2: cout 只能被5整除.n; b
11、reak; case 3: cout 可以被5,7整除.n; break; case 4: cout 只能被3整除.n; break; case 5: cout 可以被3,7整除.n; break; case 6: cout 可以被3,5整除.n; break;case 7: cout 可以被3,5,7整除.n; break; 3.6#include void main() int grade; cout grade; if(grade100|grade0) cout =90) cout =80) cout =70) cout =60) cout D.n; else cout E.n;(四)4
12、.1 (1)#include #include void main() double sum=1, t=-1, x; int i=1; cout x; do t*=(-1)*x/i; sum+=t; i+; while(fabs(t)1e-8); cout sum= sumendl;(2)#include #include void main() double sum=1, t=-1, x; cout x; int i=1; while(fabs(t)1e-8) t*=(-1)*x/i; sum+=t; i+; cout sum= sumendl;(3)#include #include vo
13、id main() double sum=1, t=-1, x; cout x; for(int i=1; fabs(t)1e-8; i+) t*=(-1)*x/i; sum+=t; cout sum= sumendl;4.2#include void main() long sum=0, t=1; for(int i=1; i=15; i+) t*=i; sum+=t; cout sum= sum endl;4.3#include void main() for(int i=1; i=9; i+) for(int j=0; j=9; j+) for(int k=0; k=9; k+) if(
14、i*i*i+j*j*j+k*k*k = 100*i+10*j+k) cout (100*i+10*j+k) 是水仙花数.n;4.4#include void main() for(int i=1; i1000; i+) int sum=0; for(int j=1; j=i/2; j+) if(i%j=0) sum+=j; if(sum=i) cout i是完数.n; 4.5#include void main() float s=100,h=100; for(int i=1; i10; i+) s+=h; h/=2; cout 共经过 s 米,第10次反弹 h 米高.n;4.6#includ
15、e void main() int peachs=1; for(int i=1; i10; i+) peachs=(peachs+1)*2; cout 第一天共摘下 peachs 个桃子.n;4.7#include #include void main() double x, a; cout a; x = a/2; while(fabs(x-a/x)/2)1e-7) x=(x+a/x)/2; cout a 的平方根是 x endl;4.8 (1)#include void main() for(int i=1; i=10; i+) for(int j=1; j=10-i; j+) cout ;
16、 for(int j=1; j=2*i-1; j+) cout #; cout endl; (2)#include void main() for(int i=1; i=8; i+) for(int j=1; j=i; j+) cout ; for(int j=1; j=18-i; j+) cout #; cout endl; 4.9 (1)#include #include void main() cout *; for(int i=1; i=9; i+) cout setw(4) i; cout n-n; for(int i=1; i=9; i+) cout setw(3) i; for(
17、int j=1; j=9; j+) cout setw(4) i*j; cout endl; (2)#include #include void main() cout *; for(int i=1; i=9; i+) cout setw(4) i; cout n-n; for(int i=1; i=9; i+) cout setw(3) i; for(int j=1; j=i; j+) cout setw(4) i*j; cout endl; (3)#include #include void main() cout *; for(int i=1; i=9; i+) cout setw(4)
18、 i; cout n-n; for(int i=1; i=9; i+) cout setw(3) i; if(i!=1) cout setw(4*i-4) ; for(int j=i; j=9; j+) cout setw(4) i*j; cout endl; 4.10#include void main() int n; long a=1, b=1, c=1, temp; cout n; for(int i=4; i=n; i+) temp=a+c; a=b; b=c; c=temp; cout c endl;(五)5.1#include #include #include bool isp
19、rime(long n);void main() /input long a,b,l=1; cout a b; cout primes from a to b is n; /processif(a=1|a=2)a=2;coutsetw(5)2; if(a%2=0) a+; for(long m=a; m=b; m+=2) if(isprime(m) /output if(l+%10=0) cout endl; cout setw(5) m; bool isprime(long n) int sqrtm=sqrt(n); for(int i=2; i=sqrtm; i+) /判明素数 if(n%
20、i=0) return false; return true;5.2#include #include #include double f(double x);double integral(double a, double b);const double eps = 1e-8;void main() double a=0, b=1; cout the integral of f(x) from a to b is n setiosflags(ios:fixed) setprecision(8) setw(8) integral(a,b) =eps) tn = t2n; in = i2n; d
21、ouble sigma = 0.0; for(int k=0; kn; k+) double x = a+(k+0.5)*h; sigma += f(x); t2n = (tn+h*sigma)/2.0; /变步长梯形 i2n = (4*t2n-tn)/3.0; /辛普生公式 n *= 2; h /= 2; return i2n;5.3#include #include void multab1();void multab2();void multab3();void main() multab1(); multab2(); multab3();void multab1() cout *; for(int i=1; i=9; i+) cout setw(4) i; cout n-n; for(int i=1; i=9; i+) cout setw(3) i; for(int j=1; j=9; j+) cout setw(4) i*j; cout endl; cout endl end
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1