C程序分析练习题

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第1页共6页C++程序设计练习题(1~5章)写出程序的运行结果3.#includeiostream.hconstintM=3,N=4;voidmain(){inti,j,s=0;for(i=1;i=M;i++)for(j=1;j=N;j++)s+=i*j;cout”s=”sendl;}4.#includeiostream.hvoidmain(){inta=2,b=5,c=0;if(a+b10)c=a*b;elsec=3*a+b;if(c=20)coutc*c;elsecout4+c-5;coutendl;a=a+b;b=a+b;c+=a+b;couta,b,c=a','b','cendl;}5.#includeiostream.hvoidmain(){intx=5;switch(2*x-3){case4:coutx’’;case7:cout2*x+1’’;case10:cout3*x-1’’;break;default:coutdefaultendl;}coutswitchend.endl;}6.#includeiomanip.h#includemath.hinta[4]={36,-5,73,8};voidmain(){inti,y;for(i=0;i4;i++){if(a[i]0)y=1;elseif(a[i]10)y=a[i]*a[i]+3;elseif(a[i]60)y=4*a[i]-5;elsey=int(sqrt(a[i]));//sqrt(x)为取x的平方根函数coutsetw(5)a[i]setw(5)y;}}7.#includeiostream.hinta[8]={36,25,20,43,12,70,66,35};voidmain(){ints0,s1,s2;第2页共6页s0=s1=s2=0;for(inti=0;i8;i++){switch(a[i]%3){case0:s0+=a[i];break;case1:s1+=a[i];break;case2:s2+=a[i];break;}}couts0’’s1’’s2endl;}8.#includeiomanip.hconstintN=5;voidmain(){inti,p=1,s=0;for(i=1;iN;i++){p=p*i;s=s+p;coutsetw(5)isetw(5)p;coutsetw(5)sendl;}}9.#includeiomanip.hconstintM=20;voidmain(){intc2,c3,c5;c2=c3=c5=0;for(inti=1;i=M;i++){if(i%2==0)c2++;if(i%3==0)c3++;if(i%5==0)c5++;}coutc2''c3''c5endl;}10.#includeiomanip.hvoidmain(){inti,j;for(i=0;i5;i++){for(j=i;j5;j++)cout’*’;coutendl;}}第3页共6页11.#includeiostream.hvoidmain(){for(inti=1,s=0;i20;i++){if(i%2==0||i%3==0)continue;couti’’;s+=i;}coutsendl;}12.#includeiostream.hconstintT=6;voidmain(){inti,j;for(i=1;i=T;i+=2)for(j=2;j=T;j+=2){if(i+jT)cout’+’;elsecout’*’;}}13.#includeiostream.hvoidmain(){inta,b,c=0;for(a=1;a4;a++)for(b=6;b1;b-=2){if((a+b)%2==0)c+=a+b;elsec+=a*b;if(b==2)couta’’b’’cendl;}}14.#includeiostream.hconstintB=2;voidmain(){intp=1,s=1;while(s50){p*=B;s+=p;}couts=sendl;}15.#includeiostream.hvoidmain(){intx=24,y=88;inti=2,p=1;do{while(x%i==0&&y%i==0){p*=i;x/=i;y/=i;}第4页共6页i++;}while(x=i&&y=i);coutp*x*yendl;}17.#includeiostream.hinta=5;voidmain(){intb=a+20;inta=10;couta''bendl;{inta=0,b=0;for(inti=1;i6;i++){a+=i;b+=a;}couta''bendl;}couta''bendl;}18.#includeiostream.hintf1(intx,inty){x=x+y;y=x+y;coutx=x,y=yendl;returnx+y;}voidmain(){intx=5,y=8;intz=f1(x,y);coutx=x,y=y;cout,z=zendl;}19.#includeiostream.hvoidf2(int&x,int&y){intz=x;x=y;y=z;}voidf3(int*x,int*y){intz=*x;*x=*y;*y=z;}voidmain(){intx=10,y=26;coutx,y=x,yendl;f2(x,y);coutx,y=x,yendl;f3(&x,&y);coutx,y=x,yendl;x++;y--;f2(y,x);coutx,y=x,yendl;}第5页共6页20.#includeiostream.hvoidf4(inta[],intn,int&s){s=0;for(inti=0;in;i++)s+=a[i];}voidmain(){intb[8]={4,8,6,9,2,10,7,12};intx;f4(b,5,x);coutx'';inty;f4(b+3,4,y);couty'';coutx+yendl;}21.#includeiostream.hvoidmain(){inta[8]={36,25,48,14,55,40,50,24};intb1,b2;b1=b2=a[0];for(inti=1;i8;i++)if(a[i]b1){b2=b1;b1=a[i];}elseif(a[i]b2)b2=a[i];coutb1''b2endl;}22.#includeiostream.hvoidmain(){chara[]=abcdabcabfgacd;inti1=0,i2=0,i=0;while(a[i]){if(a[i]==’a’)i1++;if(a[i]==’b’)i2++;i++;}couti1’’i2’’iendl;}23.#includeiostream.hvoidmain(){inta[10]={76,83,54,62,40,75,90,92,77,84};intb[4]={60,70,90,101};intc[4]={0};for(inti=0;i10;i++){intj=0;while(a[i]=b[j])j++;c[j]++;}for(i=0;i4;i++)coutc[i]’’;coutendl;}25.#includeiostream.h第6页共6页constintN=5;voidfun();voidmain(){for(inti=1;iN;i++)fun();}voidfun(){staticinta;intb=2;cout(a+=3,a+b)'';}26.#includeiostream.hvoidmain(){chars[3][5]={1234,abcd,+-*/};char*p[3];for(intI=0;I3;I++)p[I]=s[I];for(I=2;I=0;I--)coutp[I]'';coutendl;}27.#includeiostream.hvoidmain(){inti,j,len[3];chara[3][8]={year,month,day};for(i=0;i3;i++){for(j=0;j8;j++)if(a[i][j]=='\0'){len[i]=j;break;}couta[i]:len[i]endl;}}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功