您好,欢迎访问三七文档
第五章数组1.程序如下:#includeiostreamusingnamespacestd;#includecmathintmain(){inti,j,n,a[101];for(i=1;i=100;i++)a[i]=i;a[1]=0;for(i=2;i=sqrt(100);j++){if(a[i]!=0&&a[j]!=0)if(a[j]%a[i]==0)a[j]=0;}coutendl;for(i=1,n=0;i=100;i++){if(a[i]!=0){coutsetw(5)a[i];n++;}if(n==10){coutendl;n=0;}coutendl;return0;}运行结果:23571113171923293137414347535961677173798389972,程序如下:#includeiostreamusingnamespacestd;intmain(){inti,j,min,temp,a[11];coutenterdata:endl;for(i=1;i=10;i++){couta[i]=;cina[i];}coutendlTheoriginalnumbers:endl;for(i=1;i=10;i++)couta[i];coutendl;for(i=1;i=9;i++){min=i;for(j=i+1;j=10;j++)if(a[min]a[j])min=j;temp=a[i];a[i]=a[min];a[min]=temp;}coutendlThesortednumbers:endl;for(i=1;i=10;i++)couta[i];coutendl;return0;}运行结果:Enterata:a[1]=36a[2]=-9a[3]=5a[4]=6a[5]=-1a[6]=35a[7]=34a[8]=738a[9]=18a[10]=11Theoriginalnumbers:36-956-135347381811Thesortednumbers:-9-156111834367383、解:#includeiostreamusingnamespacestd;intmain(){inta[3][3],sum=0;inti,j;coutenterdata:endl;for(i=0;i3;i++)for(j=0;j3;j++)cina[i][j];for(i=0;i3;i++)sum=sum+a[i][j];coutsum=sumendl;return0;135911131517sum=274、程序如下:#includeiostreamusingnamespacestd;intmain(){inta[11]={1613172840567889100};intnum,i,j;coutarraya:endl;for(i=0;i=10;i++)couta[i];coutendl;;coutinsertdata:;cinnum;if(numa[9])a[10]=num;else{for(i=0;i=10;i++)if(a[i]num){for(j=9;j=i;j--)a[j+1]=a[j];a[i]=num;break;}}}coutNow,arraya:endl;for(i=0;i11;i++)couta[i];coutendl;return0;}运行结果:arraya:1613172840567889100insertdata:15Now,arraya:16131728405678891005、解程序如下#includeiostreamusingnamespacestd;intmain(){constintn=5;inta[n],i,temp;coutenterarraya:endl;for(i=0;in;i++)cina[i];coutarraya:endl;for(i=0;in;i++)couta[i];for(i=0;in/2;i++){temp=a[i];a[i]=a[n-i-1];a[n-i-1]=temp;}coutendlNow,arraya:endl;for(i=0;in;i++)couta[i];coutendl;return0;}运行结果:enterarraya:98765arraya:98765Now,arraya:56789Pressanykeytocontinue6、程序如下:#includeiostream#includeiomanipusingnamespacestd;intmain(){constintn=11;inti,j,a[n][n];for(i=1;in;i++){a[i][1]=1;a[i][i]=1;}for(i=3;in;i++)for(j=2;j=i-1;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for(i=1;in;i++){for(j=1;j=i;j++)coutsetw(6)a[i][j];coutendl;return0;}运行结果:11112133146415101051161520156172135352171182856705628811936841261268436917、程序如下:#includeiostream#includeiomanipusingnamespacestd;intmain(){constintn=4,m=5;inti,j,a[n][m],max,maxj;boolflag;for(i=0;in;i++)for(j=0;jm;j++)cina[i][j];for(i=0;in;i++){max=a[i][0];maxj=0;for(j=0;jm;j++)if(a[i][j]max){max=a[i][j];maxj=j;}flag=true;for(intk=0;kn;k++)if(maxa[k][maxj]){flag=flase;contine;}if(flag){couta[i][maxj]=maxendl;break;}}if(!flag)coutItdoesnotexist!endl;return0;}8、程序如下:#includeiostreamusingnamespacestd;intmain(){constintn=7;inti,number,top,bott,mid,loca,a[n];boolflag=true,sign;charc;coutenterndata:endl;cina[0];i=1;while(in){cina[i];if(a[i]=a[i-1]);i++;coutenterthisdataagain:;}coutendl;for(i=0;in;i++)couta[i];coutendl;while(flag){coutinputnumbertolookfor:;cinnumber;sign=false;top=0;bott=n-1;if((numbera[0])║(numbera[n-1]))loca=-1;while((!sign)&&(top=bott)){mid=(bott+top)/2;if(number==a[mid]){loca=mid;cout:Findnumber,itspositeisloca+1endl;sign=true;}elseif(numbera[mid])bott=mid-1;elsetop=mid+1;}if(!sign║loca==-1)coutnumberhasnotfound.endl;coutcontineornot(Y/N)?;cinc;if(c=='N'║c=='n')flag=false;}return0;}运行结果:enterdata681210Enterthisdataagian:233444455657586869769010968122334444556575868697690109Intnum,bertolookfor:77hasnotfound.contineornot(Y/N)?yInputnumbertolookfor:1212,itspositionis3Continueornot(Y/N)?n(运行结束)|9、程序如下:#includeiostreamusingnamespacestd;intmain(){intsum_day(int,int);intleap(intyear);intyear,month,day,days;coutinputdata(year,month,day):;cinyearmonthday;coutyear/month/day;days=sum_day(month,day);if(leap(year)&&month=3)days=days+1;coutisthedaysthdayinthisyear.endl;return0;}intsum_(intmonth,intday){inti;intday_tab[12]={31,28,31,30,31,30,31,31,30,31,30,31};for(i=0;imonth-1;i++)day+=day_tab[i];return(day);}intleap(intyear){intleap;leap=year%4==0&&year%100!=0||year%400==0;return(leap);}运行结果:inputdata(year,month,day):20051012005/10/1isthe274thdayinthisyear.10、程序如下:#includeiostreamusingnamespacestd;intmain(){inti,j,upper,lower,digit,space,other;chartest[3][80];upper=lower=digit=space=other=0;for(i=0;i3;i++){coutpleaseinputlinei+1endl;gets(text[i]);for(j=0;j80&&text[i][j]!='\0';j++){if(text[i][j]='A'&&text[i][j]='Z')upper++;elseif(text[i][j]='a'&&text[i][j]='z')lower++;elseif(text[i][j]='0'&&text[i][j]='9')digit++;elseif(text[i][j]=='')space++;elseother;}}coutuppercase:upperendl;coutlower:lowerendl;coutdigit:digitendl;coutspace:spaceendl;coutother:otherendl;return0;}运行结果:Pleaseinputline1:Iamaboy.Pleaseinputline2:Youareagirl;Pleaseinputline3:12+34=46dppercase:2lowercase:16digit:6space:6other:411、程序如下:#includeiostream#includestringus
本文标题:c++第五章答案
链接地址:https://www.777doc.com/doc-2904351 .html