《面向对象程序设计》综合复习题集(杨永斌整理) 下载本文

void main() {

int a=2,b=5,c=0;

if(a+b>10) c=a*b; else c=3*a+b;

if(c<=20) cout<

a=a+b; b=a+b;c+=a+b;

cout<<\ }

5. #include void main() {

int x=5;

switch(2*x-3) {

case 4: cout<

case 10: cout<<3*x-1<<’ ’; break; default: cout<<\ }

cout<<\ }

6. #include #include

int a[4]={36,-5,73,8}; void main() {

int i,y;

for(i=0; i<4; i++) { if(a[i]<0) y=1;

else if(a[i]<10) y= a[i]* a[i]+3; else if(a[i]<60) y=4*a[i]-5;

else y=int(sqrt(a[i])); // sqrt(x)为取x的平方根函数 cout<

7. #include

int a[8]={36,25,20,43,12,70,66,35}; void main() {

int s0,s1,s2; s0=s1=s2=0;

for(int i=0; i<8; i++) { switch(a[i]%3) {

case 0: s0+=a[i];break; case 1: s1+=a[i];break;

case 2: s2+=a[i];break; } }

cout<

8. #include const int N=5; void main() {

int i,p=1,s=0;

for(i=1;i

cout<

9. #include const int M=20; void main() {

int c2,c3,c5; c2=c3=c5=0;

for(int i=1; i<=M; i++) { if(i%2==0) c2++; if(i%3==0) c3++; if(i%5==0) c5++; }

cout<

10. #include void main() {

int i,j;

for(i=0;i<5;i++) {

for(j=i;j<5;j++) cout<<’*’; cout<

11. #include void main() {

for(int i=1,s=0;i<20;i++) {

if(i%2==0 || i%3==0) continue;

cout<

cout<

12. #include const int T=6; void main() {

int i,j;

for(i=1;i<=T;i+=2)

for(j=2;j<=T;j+=2) {

if(i+j

13. #include void main() {

int a,b,c=0; for(a=1;a<4;a++)

for(b=6;b>1;b-=2) {

if((a+b)%2==0) c+=a+b; else c+=a*b;

if(b==2) cout<

14. #include const int B=2; void main() {

int p=1,s=1; while(s<50) { p*=B; s+=p; }

cout<<\ }

15. #include void main() {

int x=24,y=88; int i=2,p=1; do {

while(x%i==0 && y%i==0) {

p*=i; x/=i; y/=i; } i++;

}while(x>=i && y>=i); cout<

16. #include const int N=3; void main() {

int a[N][N]={{7,-5,3},{2,8,-6},{1,-4,-2}}; int b[N][N]={{3,6,-9},{2,-8,3},{5,-2,-7}}; int i,j,c[N][N];

for(i=0;i

c[i][j]=a[i][j]+b[i][j]; for(i=0;i

cout<

17. #include int a=5;

void main() { int b=a+20; int a=10;

cout<

for(int i=1; i<6; i++) { a+=i; b+=a; }

cout<

cout<

18. #include int f1(int x, int y) {

x=x+y; y=x+y;

cout<<\ return x+y; }

void main() {

int x=5,y=8; int z=f1(x,y);

cout<<\ cout<<\ }

19. #include void f2(int& x, int& y) {

int z=x; x=y; y=z; }

void f3(int* x, int* y) {

int z=*x; *x=*y; *y=z; }

void main() {

int x=10,y=26;

cout<<\ f2(x,y);

cout<<\ f3(&x,&y);

cout<<\ x++; y--; f2(y,x);

cout<<\ }

20. #include

void f4(int a[], int n, int& s) {

s=0;

for(int i=0; i

void main() {

int b[8]={4,8,6,9,2,10,7,12}; int x; f4(b,5,x); cout<

int y; f4(b+3,4,y); cout<

21. #include void main() {