《c++程序设计》谭浩强课后习题答案_第1页
《c++程序设计》谭浩强课后习题答案_第2页
《c++程序设计》谭浩强课后习题答案_第3页
《c++程序设计》谭浩强课后习题答案_第4页
已阅读5页,还剩101页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第一章题//include<iostream>usingnamespacestd;intmain(){cout«,,This,,«,,isM;cout«naM«MC++";cout«,,program.H;return0;题//include<iostream>usingnamespacestd;intmain(){inta,b,c;a=10;b=23;c=a+b;coutvv”a+b=”;cout«c;cout«endl;return0;}七题#includc<iostream>usingnamespacestd;intmain(){inta,b,c;intfifintx,inty,intz);cin»a»b»c;c=f(a,b,c);cout«c«endl;return0;)intfifintx,inty,intz){intm;if(x<y)m=x;elsem=y;if(z<m)m=z;retum(m);)题#include<iostream>usingnamespacestd;intmain(){inta,b,c;cin»a»b;c=a+b;cout«,,a+b=,,«a+b«endl;return0;题#include<iostream>usingnamespacestd;intmain(){inta,b,c;intadd(intx,inty);cin»a»b;c=add(a,b);cout«na+b=H«c«endl;return0;}intadd(intx,inty){intz;z=x+y;retum(z);)题#include<iostream>usingnamespacestd;intmain(){charcl='a\c2=,b,,c3=,c',c4=M01',c5=M161;cout«c1«c2«c3«'\n*;cout«,,\t\b,,«c4«,\t,«c5«,\n,;return0;)题#include<iostream>usingnamespacestd;intmain(){charcl='C',c2=屮,c3='+';cout«HIsay:\Mn«cl«c2«c3«V,;cout«,,\t\t,,«HHesays:ヽ”CHisveryinteresting!ヽ””"V;return0;}题#include<iostream>usingnamespacestd;intmain(){intij,m,n;i=8;j=10;m=++iザ++;n=(++i)+(+tD+m;coutwivv・ヽt'vqw・ヽt'vvmvvへビvvnwendl;return0;}题#include<iostream>usingnamespacestd;intmain(){charcl-C\c2=,h\c3-i*,c4=*n',c5-a*;cl+=4;c2+=4;c3+=4;c4+=4;c5+=4;cout«Hpasswordis:M«cl«c2«c3«c4«c5<<endl;return0;3.2题//include<iostream>#include<iomanip>usingnamespacestd;intmain(){floath,r,l,s,sq,vq,vz;constfloatpi=3.1415926;cout«npleaseenterr,h:M;cin»r»h;I=2*pi*r;s=r*r*pi;sq=4*pi*r*r;vq=3.0/4.0*pi*r*r*r;vz=pi*r*r*h;cout«setiosflags(ios::fixed)«setiosflags(ios::right)«setprecision(2);cout«Ml=M«setw(10)«l«endl;cout«Ms=M«setw(10)«s«endl;cout«,,sq=n«setw(10)«sq«endl;cout«Mvq=,,«setw(10)«vq«endl;cout«Hvz=M«setw(10)«vz«endl;return0;}题#include<iostream>usingnamespacestd;intmain(){floatc,f;coutw”请输入ー个华氏温度:“;cin»f;c=(5.0/9.0)*(f-32); 〃注意5和9要用实型表示,否则5/9值为〇coutvv”摄氏温度为アvvc«endl;return0;};题#include<iostream>usingnamespacestd;intmain(){charcl,c2;coutw”请输入两个字符cl,c2:M;cl=getchar(); 〃将输入的第一个字符赋给clc2=getchar(); 〃将输入的第二个字符赋给c2coutw”用putchar函数输出结果为ア;putchar(c1);putchar(c2);cout«endl;coutw”用cout语句输出结果为:“;cout«c1«c2«endl;return0;}usingnamespacestd;intmain(){charcl,c2;coutw”请输入两个字符cl,c2:M;cl=getchar(); 〃将输入的第一个字符赋给clc2=getchar(); 〃将输入的第二个字符赋给c2coutw”用putchar函数输出结果为:“;putchar(c1);putchar(44);putchar(c2);cout«endl;coutvv”用cout语句输出结果为:“;cout«cl«M,"«c2«endl;return0;}题#include<iostream>usingnamespacestd;intmain(){charcl,c2;intil,i2; 〃定义为整型coutw”请输入两个整数il,i2:”;cin»il»i2;cl=il;c2=i2;coutw”按字符输出结果为:“we1vv”Jwc2vvendl;return0;}题#include<iostream>usingnamespacestd;intmain(){inta=3,b=4,c=5,x,y;cout«(a+b>c&&b=c)«endl;cout«(a||b+c&&b-c)«endl;cout«(!(a>b)&&!c||l)«endl;cout«(!(x=a)&&(y=b)&&0)«endl;cout«(!(a+b)+c-l&&b+c/2)«endl;return0;}题include<iostream>usingnamespacestd;intmain(){inta,b,c;cout«"pleaseenterthreeintegernumbers:";cin»a»b»c;if(a<b)ifi[b<c)cout«"max="«c;elsecout«"max="«b;elseif(a<c)cout«"max="«c;elsecout«"max="«a;cout«endl;return0;}另ー解#include<iostream>usingnamespacestd;intmain(){inta,b,c,temp,max;cout«Hpleaseenterthreeintegernumbers:";cin»a»b»c;temp=(a>b)?a:b; /・将a和b中的大者存入temp中・/max=(temp>c)?temp:c; /・将a和b中的大者与c比盘,最大者存入max*/cout«"max="«max«endl;return0;}题#includc<iostream>usingnamespacestd;intmain(){intx,y;cout«"enterx:";cin»x;if(x<l){y=x;cout«"x="«x<<",y=x="«y;elseif(x<10) //l<x<10{y=2*x-l;cout«"x="«x<<",y=2*x-l="«y;)else //x>10{y=3*x-ll;cout«"x="«x<<",y=3*x-ll="«y;)cout«endl;return0;}题#include<iostream>usingnamespacestd;intmain(){floatscore;chargrade;cout«"pleaseenterscoreofstudent:";cin»score;while(score>100||score<0){cout«"dataerror,enterdataagain.";cin»score;}switch(int(score/l0)){case10:case9:grade='A';break;case8:grade=,B';break;case7:grade='C;break;case6:grade='D';break;default:grade-E*;|cout«Mscoreis”vvscore<vゝgradeisM«grade«endl;return0;}3.12题#include<iostream>usingnamespacestd;intmain(){longintnum;intindiv,ten,hundred,thousand,tenthousand,place;/・ハ别代表个位,十位,百位,千位,万位和位数*/cout«Henteraninteger(〇〜99999):";cin»num;if(num>9999)place=5;elseif(num>999)place=4;elseif(num>99)place=3;elseif(num>9)place=2;elseplace=1;cout«Hplace="«place«endl;〃计算各位数字ten_thousand=num/10000;thousandsint)(num-ten_thousand*l0000)/1000;hundred=(int)(num-ten_thousand*10000-thousand*l000)/100;ten=(int)(num-ten_thousand*10000-thousand*l000-hundred*l00)/10;indiv=(int)(num-ten_thousand*10000-thousand*l000-hundred*100-ten*l0);cout«Horiginalorder:M;switch(place){case5:cout«ten_thousand«M,,,«thousand«,,,,,«hundred«,7,«ten«,,,,,<<indiv«endl;cout«"reverseorder:";cout«indiv«ten«hundred«thousand«ten_thousand«endl;break;case4:cout«thousand«","«hundred«","«ten«","«indiv«endl;cout«"reverseorder:";cout«indiv«ten«hundrcd«thousand«endl;break;case3:cout«hundred«","«ten«","«indiv«endl;cout«"reverseorder:";cout«indiv«ten«hundred«endl;break;case2:cout«ten«","«indiv«endl;cout«"reverseorder:";cout«indiv«ten«endl;break;case1:cout«indiv«endl;cout«"reverseorder:";cout«indiv«endl;break;return0;3.13题#includc<iostream>usingnamespacestd;intmain(){longi;//i为利润floatbonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+100000*0.05;3.13题#includc<iostream>usingnamespacestd;intmain(){longi;//i为利润floatbonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+100000*0.05;bon6=bon4+100000*0.03;bon1〇=bon6+400000*0.015;cout«Menteri:H;cin»i;if(i<=100000)〃利润为10万元时的奖金〃利润为20万元时的奖金〃利润为40万元时的奖金〃利润为60万元时的奖金〃利润为100万元时的奖金bonus=i*0.1;elseif(i<=200000)bonus=bonl+(i-l00000)*0.075;elseif(i<=400000)bonus=bon2+(i・200000)*0.05;elseif(i<=600000)bonus=bon4+(i-400000)*0.03;elseif(i<=1000000)bonus=bon6+(i-600000)*0.015;elsebonus=bon10+(i-1000000)*0.01;cout«"bonus=,,«bonus«endl;return0;〃利润在10万元以内按10%提成奖金〃利润在10万元至20万时的奖金〃利润在20万元至40万时的奖金〃利润在40万元至60万时的奖金〃利润在60万元至100万时的奖金〃利润在100万元以上时的奖金3.13题另一解#include<iostream>usingnamespacestd;intmain(){longi;floatbonus,bon1,bon2,bon4,bon6,bon10;intc;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon1〇=bon6+400000*0.015;cout«Menteri:";cin»i;c=i/l00000;if(c>10)c=10;switch(c){case0:bonus=i*0.1;break;bonus=bonl+(i-100000)*0.075;break;bonus=bon2+(i-200000)*0.05;break;bonus=bon4+(i-400000)*0.03;break;bonus=bon6+(i-600000)*0.015;break;bonus=bon10+(i-1000000)*0.01;}cout«,,bonus=,,«bonus«endl;return0;}题#include<iostream>usingnamespacestd;intmain(){intt,a,b,c,d;cout«,,enterfournumbers:M;cin»a»b»c»d;cout«na=,,«a«M,b=M«b«n,c=,,«c«,,,d=,,«d«endl;if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(a>d){t=a;a=d;d=t;}if(b>c){t=b;b=c;c=t;}if(b>d){t=b;b=d;d=t;}if(c>d){t=c;c=d;d=t;}cout«"thesortedsequence:M«endl;cout«a«H,,,«b«H, M«d«endl;return0;)题#include<iostream>usingnamespacestd;intmain(){intp,r,n,m,temp;cout«npleaseentertwopositiveintegernumbersn,m:',;cin»n»m;if(n<m){temp=n;n=m;m=temp; 〃把大数放在n中,小数放在m中p=n*m; 〃先将n和m的乘积保存在p中,以便求最小公倍数时用while(m!=0) 〃求n和m的最大公约数{r=n%m;n=m;m=r;}cout«HHCF="«n«endl;cout<<HLCD=M«p/n«endl;//p是原来两个整数的乘积return0;题#includc<iostrcam>usingnamespacestd;intmain(){charc;intletters=0,space=0,digit=0,other=0;cout«nenteroneline::M«endl;while((c=getchar())!=へn'){if(c>='a'&&cv='z'||c>='A'&&c<=,Z,)letters-H-;elseif(c=**)space-H-;elseif(c>='0'&&c<=9)digit++;elseotheri-4-;}coutvv”lette亡"vvlettersw”,space:”vvspace〈v",digit”vvdigitvv”,other:"«other«endl;return0;题#include<iostream>usingnamespacestd;intmain(){inta,n,i=1,sn=0,tn=0;cout«na,n=:";cin»a»n;while(i<=n){tn=tn+a;〃赋值后的tn为i个a组成数的值sn=sn+tn; 〃赋值后的sn为多项式前i项之和a=a*10;++i;)cout«,,a4-aa+aaa+...=,,«sn«endl;return0;}题#include<iostream>usingnamespacestd;intmain(){floats=O,t=l;intn;for(n=l;n<=20;n+-i-)(t=t*n; /Z求n!s=s+t; //将各项累加}cout«M1!+2!+...+20!=H«s«endl;return0;题#include<iostream>usingnamespacestd;intmain(){intij,k,n;

cout«,,narcissusnumbersare:M«endl;fbr(n=100;n<1000;n-H-){i=n/100;j=n/10-i*10;k=n%10;if(n=i*i*i4-j*j*j+k*k*k)cout«n«ncout«endl;return0;}题kl0=i;break; 〃找出的笫kl0=i;break; 〃找出的笫10个因子赋给klOintk15k2,k3,k4,k5,k6,k7,k8,k9,k10;inti,a,n,s;fbr(a=2;a<=m;a-H-)〃a是23000之间的整数,检查它是否为完数{n=0;〃n用来累计a的因子的个数s=a;〃s用來存放尚未求出的因子之和,开始时等于afbr(i=l;i<a;i++)/Z检查i是否为a的因子if(a%i=0)//如果i是a的因子{n++;〃n加1,表示新找到ー个因子s=s-i;〃5减去已找到的因子,s的新值是尚未求出的因子之和switch(n)/Z将找到的因子赋给kl,...,klO{case1:kl=i;break;/Z找出的笫1个因子赋给klcase2:k2=i;break;/Z找出的笫2个因子赋给k2case3:k3=i;break;/Z找出的笫3个因子赋给k3case4:k4=i;break;/Z找出的笫4个因子赋给k4case5:k5=i;break;〃找出的笫5个因子赋给k5case6:k6=i;break;/Z找出的笫6个因子赋给k6case7:k7=i;break;/Z找出的笫7个因子赋给k7case8:k8=i;break;/Z找出的笫8个因子赋给k8case9:k9=i;break;/Z找出的笫9个因子赋给k9usingnamespacestd;intmain(){constintm=1000;/Z定义寻找范围case10:))if(s=0) //s=0表示全部因子都已找到了{cout«a«Hisa完数”〈〈endl;cout«Mitsfactorsare:“;if(n>l)cout«kl«n,H«k2;//n>l表示a至少有2个因子if(n>2)coutvv”,”wk3;〃n>2表示至少有3个因子,故应再输出ー个因子if(n>3)coutw”,”vvk4;〃n>3表示至少有4个因子,故应再输出ー个因子if(n>4)coutvv”,“vvk5;/Z以下类似if(n>5)cout«,,,,,«k6;if(n>6)cout«M,"«k7;if(n>7)cout«M,n«k8;if(n>8)cout«M,M«k9;if(n>9)cout«M,M«klO;cout«endl«endl;return0;)3.20题另一解//include<iostream>usingnamespacestd;intmain(){intm,s,i;for(m=2;m<1000;m++){s=0;for(i=l;i<m;i++)if((m%i)==0)s=s+i;if(s=m){cout«m«Misa完数”《endl;cout«"itsfactorsare:**;for(i=1;i<m;i++)if(m%i==0)cout«i«"n;cout«endl;))return0;}题另ー解#includc<iostream>usingnamespacestd;intmain(){intk[ll];inti,a,n,s;for(a=2;a<=1000;a-H-){n=0;s=a;for(i=l;i<a;i++)if((a%i)=0){n++;s=s-i;k[n]=i; //将找到的因子赋给k[l卜-k[10]}if(s=0){cout«a«nisa完数”v〈endl;cout«*'itsfactorsare:”;for(i=l;i<n;i-H-)cout«k[i]«MM;cout«k[n]«endl;return0;题#include<iostream>usingnamespacestd;intmain(){inti,t,n=20;doublea=2,b=l,s=0;for(i=l;i<=n;i++){s=s+a/b;t=a;a=a+b; /Z将前ー项分子与分母之和作为下ー项的分子b=t; /Z将前ー项的分子作为下一项的分母)cout«**sum=',«s«endl;return0;)题#include<iostream>usingnamespacestd;intmain(){intday,xl,x2;day=9;x2=l;while(day>0){xl=(x2+l)*2; //第1天的桃子数是第2天桃子数加1后的2倍x2=xl;day—;}cout«,,total=,,«x1«endl;return0;)题#include<iostream>#include<cmath>usingnamespacestd;intmain(){floata,x0,xl;cout«Henterapositivenumber:M;cin»a; /Z输入a的值x0=a/2;xl=(x0+a/x0)/2;do{x0=xl;xl=(x0+a/x0)/2;)while(fabs(xO-x1)>=1e-5);cout«HThesquarerootofM«a«Mis"«xl«endl;return0;)题#includc<iostream>usingnamespacestd;intmain(){inti,k;fbr(i=0;i<=3;i++) /Z输出上面4行・号{for(k=0;k<=2*i;k-H-)cout«"*"; /Z输出・号cout«endl; 〃输出完一行・号后换行for(i=0;i<=2;i++)/Z输出下面3行・号{for(k=0;k<=4-2*i;k++)cout«"*"; /Z输出・号cout«endl; //输出完一行・号后换行return0;题//include<iostream>usingnamespacestd;intmain(){charij,k; /*i是a的对手;j是b的对手;k是c的对手・/fbr(i=X;iv=Z;i++)if(i!=j)fbr(k=,X';k<=,Z';k-H-)if(i!=k&&j!=k)if(i!='X'&&k!=X&&k!='Z')cout«,,A-,,«i«M C-"«k«endl;return0;题#include<iostream>usingnamespacestd;intmain(){inthcf{int,int);intlcd(int,int,int);intu,v,h,l;cin»u»v;h=hcfl(u,v);cout«,,H.C.F=,,«h«endl;l=lcd(u,v,h);cout«HL.C.E)=H«l«endl;return0;inthcfifintu,intv){intt,r;if(v>u){t=u;u=v;v=t;}while((r=u%v)!=0){u=v;v=r;}retum(v);)intlcd(intu,intv,inth){retum(u*v/h);题//include<iostream>#include<math.h>usingnamespacestd;floatxl,x2,disc,p,q;intmain(){voidgreater_than_zero(float,float);voidcqual_to_zero(float,float);voidsmaller_than_zero(float,float);floata,b,c;cout«Minputa,b,c:H;cin»a»b»c;disc=b*b-4*a*c;cout«nroot:M«endl;if(disc>0)greater_than_zero(a,b);cout«nx1=M«x1<<M,x2=,,«x2«endl;)elseif(disc==0){equal_to_zero(a,b);cout«Mx1=n«x1<<n,x2=M«x2«endl;)else{smaller_than_zero(a,b);cout«,,xl-,«p«,,+"«q«,,i"«endl;cout«,,x2=,,«p«,,-,,«q«,'i,,«endl;}return0;}voidgreater_than_zero(floata,floatb) /・定义ー个函数,用来求disc>0时方程的根・/"{x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);voidequaltozero(floata,floatb)/・定义ー个函数,用来求disc=0时方程的根・/(xl=x2=(-b)/(2*a);)voidsmaller_than_zero(floata,floatb)/・定义ー个函数,用来求disc〈0时方程的根・/-(p=-b/(2*a);q=sqrt(-disc)/(2*a);}题#includc<iostream>usingnamespacestd;intmain(){intprime(int); /・函数原型声明・/intn;cout«Hinputaninteger:n;cin»n;if(prime(n))cout«n«Hisaprime.M«endl;elsecout«n«Hisnotaprime.n«endl;return0;|intprime(intn){intflag=l,i;for(i=2;i<n/2&&flag==l;i++)if(n%i=0)flag=0;retum(flag);题#includc<iostream>usingnamespacestd;intmain(){intfac(int);inta,b,c,sum=0;cout«Mentera,b,c:H;cin»a»b»c;sum=sum+fac(a)4-fac(b)-i-fac(c);cout«a«"!+M«b«l,!4-"«c«,,!=n«sum«endl;return0;}intfac(intn){intf=l;for(inti=l;i<=n;i-H-)f=f*i;returnf;)题//include<iostream>#includc<cmath>usingnamespacestd;intmain(){doublee(double);doublex,sinh;cout«Henterx:";cin»x;sinh=(e(x)+e(-x))/2;cout«,,sinh(,,«x«,,)=,,«sinh«endl;return0;doublee(doublex){returnexp(x);}题〃牛顿迭代法#includc<iostream>#include<cmath>usingnamespacestd;intmain(){doublesolut(double,double,double,double);doublea,b,c,d;cout«Minputa,b,c,d:H;cin»a»b»c»d;cout«nx=M«solut(a,b,c,d)«endl;return0;}doublesolut(doublea,doubleb,doublec,doubled){doublex=l,x0,f,fl;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;fl=(3*a*x0+2*b)*x0+c;x=x0-f7fl;}while(fabs(x-x0)>=le-5);retum(x);intGcd_2(inta,intb)/Z欧几里德算法求a,b的最大公约数 {if(a<=0||b<=0)〃预防错误return0;inttemp;while(b>0)//b总是表示较小的那个数,若不是则交换a,b的值{temp=a%b;〃迭代关系式 a=b;//a是那个胆小鬼,始终跟在b的后面 b=temp;//b向前冲锋占领新的位置 }returna;}题//include<iostream>#include<cmath>usingnamespacestd;intmain(){voidgodbaha(int);intn;cout«Hinputn:“;cin»n;godbaha(n);return0;)voidgodbaha(intn){intprime(int);inta,b;fbr(a=3;a<=n/2;a=a+2){if(prime(a)){b=n-a;if(prime(b))cout<<n«,-M«a«,,4-M«b«endl;}intprime(intm){inti,k=sqrt(m);fbr(i=2;i<=k;i-H-)iRm%i==O)break;if(i>k)return1;elsereturn0;题//递归法#include<iostream>usingnamespacestd;intmain(){intx,n;floatp(int,int);cout«Hinputn&x:H;cin»n»x;cout«,,n=,,«n«,,,x=u«x«endl;;cout«,,PM«n«,,(x)=,,«p(n,x)«endl;return0;floatp(intn,intx){if(n==0)retum(l);elseif(n=l)retum(x);elseretum(((2*n-1)*x-p((n-1),x)-(n-1)*p((n-2),x))/n);|题〃汉诺塔问题#include<iostream>usingnamespacestd;intmain(){voidhanoi(intn,charone,chartwo,charthree);intm;cout«ninputthenumberofdiskes:**;cin»m;cout«HThestepsofmovingM«m«ndisks:M«endl;hanoi(m,'A',B,'C);return0;voidhanoi(intn,charone,chartwo,charthree)〃将n个盘从one座借助two座,移到three座{voidmove(charx,chary);i[n=l)move(one,three);else{hanoi(n-1,one,three,two);move(one,three);hanoi(n-1,two,one,three);voidmove(charx,chary){cout«x«M->n«y«endl;}题#include<iostream>usingnamespacestd;intmain(){voidconvert(intn);intnumber;cout«Hinputaninteger:M;cin»number;cout«noutput:n«endl;if(number<0){cout«"-M;number=-number;convert(number);cout«endl;return0;}voidconvert(intn)〃感觉根本想不出的么{inti;charc;if((i=n/10)!=0)convert(i);c=n%10+’0';cout«nn«c;题#includc<iostream>usingnamespacestd;intmain(){intfifint);intn,s;cout«"inputthenumbern:M;cin»n;s=f(n);cout«"Theresultis"«s«endl;return0;}int[intn){;if(n=l)return1;elsereturn(n*n+fi(n-l));题#include<iostream>#includc<cmath>usingnamespacestd;#defineS(a,b,c)(a+b+c)/2#defineAREA(a,b,c)sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c))intmain(){floata,b,c;cout«Minputa,b,c:n;cin»a»b»c;if(a+b>c&&a+c>b&&b+c>a)cout<<Harea=,,«AREA(a,b,c)«endl;elsecout«HItisnotatriangle!M«endl;return0;}题#include<iostream>usingnamespacestd;//#defineLETTER1intmain(){charc;cin»c;#ifLETTERif(c>=,a,&&c<=,z,)c=c-32;#elseif(c>=W&&c〈=Z)c=c+32;#endifcout«c«endl;return0;}题//include<iostream>usingnamespacestd;#defineCHANGE1intmain(){charch[40];cout«ninputtext:M«endl;;gets(ch);#if(CHANGE){for(inti=0;i<40;i++){if(ch[i]!=ヘ0')if(ch[i]>='a'&&ch[i]<'z'||ch[i]>'A'&&ch[i]<'Z')ch[i]+=l;elseif(ch[i]='z'||ch[i]=-Z')ch[i]-=25;#endifcout«"output:"«endl«ch<<endl;return0;)题file#include<iostream>usingnamespacestd;inta;intmain(){externintpower(int);intb=3,c,d,m;cout«**enteranintegeraanditspowerm:n«endl;cin»a»m;c=a*b;cout«a«,,*M«b«,,=,,«c«endl;d=power(m);cout«a«M**H«m«,,=M«d«endl;return0;}题fileexterninta;intpower(intn){inti,y=l;fbr(i=l;i<=n;i-H-)y*=a;returny;题#include<iostream>usingnamespacestd;intmain(){coutvv2w'for(inti=3;i<=100;i-H-){boolt=true;for(inta=2;a<i;a-H-)if(i%a==0){t=false;break;}if⑴cout«i«**;}return0;)#includc<iostream>//include<iomanip>usingnamespacestd;#include<math.h>intmain(){intij,n,a[101];for(i=l;i<=100;i-H-)a[i]=i;a[l]=0;for(i=2;i<sqrt(100);i-H-)for(j=i+l;j<=100;j-H-){if(a[i]!=O&&a[j]!=O)if(aU]%a[i]==0)aU]=O;}cout«endl;for(i=l,n=0;i<=100;i++){if(a[i]!=0){cout«setw(5)«a[i]«Mn++;}if(n=10){cout«endl;n=0;}}cout«endl;return0;题#include<iostream>usingnamespacestd;//#include<math.h>intmain(){inti,j,min,temp,a[l1];cout«Henterdata:M«endl;for(i=l;i<=10;i++){cout«"a[M«i«"]=H;cin»a[i]; 〃输入10个数)cout«endl«"Theoriginalnumbers:M«endl;;for(i=l;i<=10;i-H-)cout«a[i]«M /Z输出这10个数cout«endl;;for(i=l;i<=9;i++) 〃以下8行是对10个数排序{min=i;forO=i+ljv=10;j++)if(a[min]>a[j])min=j;temp=a[i]; 〃以下3行将a[i+l]〜a[10]中最小者与a[i!对换a[i]=a[min];a[min]=temp;}cout«endl«MThesortednumbers:M«end1;for(i=l;i<=10;i++) /Z输出已排好序的10个数cout«a[i]«"cout«endl;return0;}#include<iostream>usingnamespacestd;intmain(){inta[10],e,k,t;for(inti=0;i<l0;i++){cin»a[i];}for(inti=0;i<9;i++){k=i;for(t=i+l;t<10;t++){if(a[i]<a[t])k=t;}e=a[i];a[i]=a[k];a[k]=e;}for(inti=0;i<10;i-H-){cout«a[i]«*';}return0;#include<iostream>#include<iomanip>usingnamespacestd;intmain(){inta[10][10];for(inti=0;i<10;i++){for(intt=0;t<10;t++)a[i][t]=O;}for(inti=0;i<10;i++)a[i][i]=l;for(inti=0;i<10;i-H-)a[i][0]=l;for(inti=2;i<10;i++){for(intt=l;t<=i-l;t++)a[i][t]=a[i-l][t-l]+a[i-l][t];}for(inti=0;i<10;i++){for(intt=0;t<10;t-H-){if(a[i][t]!=O)cout«a[i][t]«・ヒ}cout«endl;}return0;}我的第一个带指针的C++程序,呵呵ー#include<iostream>usingnamespacestd;intmain(){char*p;inti=0;charc[100];gets(c);p=c;while(*p!=へ〇'){i++;p++;}cout«i;return0;题#include<iostrcam>usingnamespacestd;intmain(){inta[3][3],sum=O;intij;cout«ftenterdata:"«endl;;for(i=0;i<3;i-H-)for(j=0J<3

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论