《c++基础例题100道.doc》由会员分享,可在线阅读,更多相关《c++基础例题100道.doc(49页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、/* 1153153 = 13 + 53 + 33 */#includevoid main()int i, a=0, b=0, c=0;for(i=100;i1000;i+)a=i%10;b=i/10%10;c=i/100%10;if(a*a*a+b*b*b+c*c*c=i)couti=iendl;/* 261236 = 1 + 2 + 3610006 -123 */#includevoid main()int i,j,sum=0,a50,k,t;for(i=1;i=1000;i+)sum=0;for(j=1;ji;j+)if(i%j=0)sum+=j;ak+=j;t=k;if(sum=i)c
2、outi;for(k=0;kt;k+)coutak;if(kt-1)cout,;coutendl;k=0;/* 3Sn=a+aa+aaa+aaaa2+22+222+22222n=5n*/#includevoid main()double a,sn=0.0,sum=0.0;int n,i;couta;coutn;sn=a;sum=a;for(i=2;i=n;i+)sum=sum*10+a;sn+=sum;coutSn=snendl;/* 41001010*/#includevoid main()double h1=100,h2=100,sum=0.0;int i;for(i=1;i=10;i+)
3、sum+=h2;h1=h1/2.0;h2=h1*2;coutsum=sum h1=h1endl;/* 510*/#includevoid main()int number,i;number=1;for(i=10;i1;i-)number=(number+1)*2;coutnumber=numberendl;4 /* 110*/#includevoid main()int i,j,temp,a10;coutplease input ten numbers:n;for(i=0;iai;for(i=0;i10;i+) /for(j=i+1;jaj)temp=aj;aj=ai;ai=temp;cout
4、resort result=;for(i=0;i10;i+)coutai 1) */#includedouble fun (double,double);void main()double n,x,sum;coutinput n and xnx;sum=fun(n,x);coutPn(x)=sum1)return (2*n1-1)*x1*fun(n1-1,x1)-(n1-1)*fun(n1-2,x1)/n1;/* 3 */#includevoid judge(char a);void main()const int size=100;char asize;cin.getline(a,size)
5、;judge(a);void judge(char a100)/int letter=0,number=0,others=0,i=0;while(ai!=0)if (ai=a&ai=A&ai=0 & ai=9) number+;/else others+;/i+;coutletter=letter number=number others=othersendl;/* 4 */#includeint lead(int);void main()int ly,year,month,date,i,sum=0;coutyearmonthdate;int a12=31,0,31,30,31,30,31,3
6、1,30,31,30,31;ly=lead(year);if (ly=1)a1=29;/366else a1=28;/365for(i=0;imonth-1;i+) /sum+=ai;sum+=date; /coutsum;int lead(int y)/if(y%4=0&y%100!=0)|(y%400=0) return 1;/else return 0;/* 5 */#includeint cdivisor(int,int);int cmultiple(int,int,int);void main()int x,y,d,m;coutxy;d=cdivisor(x,y);m=cmultip
7、le(x,y,d);coutcommon divisor is dendlcommon multiple is mendl;int cdivisor(int x1,int y1)/int r,temp;if (x1y1)temp=x1;x1=y1;y1=temp;while(x1%y1)/0r=x1%y1;x1=y1;y1=r;return y1;int cmultiple(int x2,int y2,int d1)/return x2*y2/d1;/* 6 */#include#includevoid main()const int size=100;char asize,bsize;cou
8、tinput two string:endl;cin.getline(a,size);cin.getline(b,size);strcat(a,b);couta=aendl;/* 7 */#include#includevoid scpy(char *,char *);void main()const int size=100;char asize=Hello world;char bsize=Net;couta= ab= bendl;scpy(a,b);couta= aendl;void scpy(char *p,char *q)while(*q!=0)if (*q=a|*q=A|*q=e|
9、*q=E|*q=i|*q=I|*q=o|*q=O|*q=u|*q=U)* p+=*q;q+;/* 8419901 9 9 0 */#include#includevoid outs(char a);void main()const int size=10;char asize;cin.getline(a,size);outs(a);void outs(char a10)int i;if(strlen(a)=4)for(i=0;i4;i+)coutai ;else coutinput error.endl;5 /* 1abcddcba */#includevoid back(char *);vo
10、id main()char a50=abcdefg;couta=aendl;back(a);#includevoid back(char *p)int i=0;while(*p!=0)p+;/i+;/cout0;i-)/p-;cout*p;coutendl;/* 215(1) */#includevoid tri(int a15);void main()int i,j,a1515;tri (a);couta= ;for(i=0;i15;i+)/for(j=0;j=i;j+)cout=1&aij=9)/4cout=10&aij=99)/3cout=100&aij=999)/2cout ;else
11、 cout ;/1coutendl ;/void tri(int a1515)int i,j;for(i=0;i15;i+)for(j=0;j=i;j+)if(j=0|j=i)/1aij=1;else aij=ai-1j-1+ai-1j;/* 3strcat */#include#includevoid scat(char *,char *);void main()const int size=100;char asize=Hello;char bsize=Bye;couta=a b=bendl;scat(a,b);couta=a after link a and bendl;void sca
12、t(char *p,char *q)while(*p!=0)/ap+;while(*q!=0)* p=*q;p+;q+;/* 48 1 63 5 74 9 2 1n2 */N#includevoid square(int a10,int k,int n);void main()int n,i,j,k,a1010=0;coutinput an odd number:n;k=n/2;/square(a,k,n);for(i=0;in;i+)for(j=0;jn;j+)couttaij;coutendl;void square(int a10,int k,int n)int i,j;for(i=1,
13、j=0;i=n*n;i+,j-,k+)/n1if(j=n)/j+=2;k-=1;else if(j=n)/k-=n;else if(ajk!=0)/j+=2;k-=1;ajk=i;/N#include #include #include using namespace std;void printA(int *p,int n)/ncoutendln:endl;int i,j;for(i = 0;i n;i+)for(j = 0;j n;j+)coutsetw(4)pij;coutendl;coutendlendl;bool Judge(int *p,int n)/nint i,j,sum =
14、0,NowSum = 0;bool YesOrNo = true;for(j = 0;j n;j+)/sum += p0j;for(i = 1;i n;i+)/NowSum = 0;for(j = 0;j n;j+)NowSum += pij;if(NowSum != sum)YesOrNo = false;goto END;for(i = 0;i n;i+)/NowSum = 0;for(j = 0;j n;j+)NowSum += pji;if(NowSum != sum)YesOrNo = false;goto END;NowSum = 0;for(i = 0,j = 0;i n,j =
15、 0,j n;i-,j+)/NowSum += pij;if(NowSum != sum)YesOrNo = false;goto END;END:return YesOrNo;void combination(int *p,int n,int *a)/m = n*n(1,2,3.m)int m = n*n;static int Num = 0;int *b_val = new intm;int c = 0,k,i,j;b_valc = -1;/-1 c0-1while(b_val0 m) /-1if(+b_valc m) /0/b00 10 11 20 21 22.66.70.77 80.8
16、8 89 012345678/ 78 80.87 88 89 012345687/ 67.70.76 88 012345768/ 012345786/ 012345867 ./ 876543210for(k = 0;k c;k+) /KCif(b_valk = b_valc)break;if(k = c)/if(c+1 m) /1 2 3.7 8 / 8+c; /1 2 3.7 8b_valc = -1;/ continue;else /k = -1;for(i = 0;i n;i+)for(j = 0;j n;j+)pij = ab_val+k; /a0-a8/nif(Judge(p,n)p
17、rintA(p,n);elsec-;delete b_val;void evaluate(int *p,int n)/nint i;int *AllNum = new int3*3;for(i = 1;i = n*n;i+)AllNumi - 1 = i;combination(p,n,AllNum);delete AllNum;void main()int i,n,*a;string s;do/nncouts;if(!strcmp(s.c_str(),e) | !strcmp(s.c_str(),E)break;else if(s.find_first_not_of(0123456789)
18、!= string:npos)coutendl;continue;elsen = atoi(s.c_str();if(n1)cout= 1endl;continue;/a = new int*n;for(i = 0; i n; i+)ai = new intn;coutendl;/nevaluate(a,n);coutendl;for(i = 0; i n; i+)delete ai;delete a;while(1);return;/* 53x3*/#includeint dia(int a3);void main()int i,j,sum,a33=2,3,5,6,2,3,1,9,0;cou
19、ta= ;for(i=0;i3;i+)for(j=0;j3;j+)coutaij;coutendl ;sum=dia(a);coutnsum=sumendl;int dia(int a33)int i,j,sum=0;for(i=0;i3;i+)/for(j=i;j=i;j+)sum+=aij;for(j=0;j3;j+)/for(i=2-j;i=2-j;i+)if(i!=j)/sum+=aij;return sum;/* 6s2s1strcpy0 0*/#includevoid scopy(char a,char b);void main()int i;char a10;char b10=H
20、ello;scopy(a,b);for(i=0;ai!=0;i+)coutai;coutendl;void scopy(char a,char b)int i;for(i=0;bi!=0;i+)ai=bi;ai=0;/* 71001()*/#includevoid main()int i,j;for(i=1;i=100;i+)for(j=2;ji;j+)/if(i%j!=0);else break;/if(i=j)/cout i;coutendl;/* 810*/#includevoid csort(int a10);void main()int i;int a10=6,4,2,7,9,0,1
21、,6,3,0;for(i=0;i10;i+)/coutai;coutendl;csort(a);for(i=0;i10;i+)/coutai;coutendl;void csort(int a10)/int i,j,k,temp=0;for(i=1;i10;i+)k=i;for(j=k+1;jaj)k=j;if(k!=i)temp=ai;/ai=ak;ak=temp;6 /* 11010*/#includevoid psort(int *p);void main()int i,a10;coutplease input ten numbers:n;for(i=0;iai;psort(a);cou
22、tresort result=;for(i=0;i10;i+)coutai ;void psort(int *p)int i,j,temp;for(i=0;i10;i+) /for(j=i+1;jpj)temp=pj;pj=pi;pi=temp;/* 2A123x456 1233?8997jhlklla*/#include#include#include int charge(int *,char *);void main()int a50,i,numb;char b50;coutplease input a character string:endl;cin.getline(b,50);system(cls);coutyour character string is ;cout.write(b,strlen(b)endl;numb=charge(a,b);for(i=0;inumb;i+)cout ai=ai;coutendltotal numbers=numb=0&*p=0&*p=9)/* q=(*q)*10+(*p)-0);/p+;q+;num