C++程序设计源代码大全.docx
- 文档编号:27870449
- 上传时间:2023-07-05
- 格式:DOCX
- 页数:53
- 大小:23.25KB
C++程序设计源代码大全.docx
《C++程序设计源代码大全.docx》由会员分享,可在线阅读,更多相关《C++程序设计源代码大全.docx(53页珍藏版)》请在冰豆网上搜索。
C++程序设计源代码大全
S2_1
#include
usingnamespacestd;
voidmain()
{
floata,b,c;
cout<<"inputabc\n";
cin>>a>>b>>c;
if((a+b cout<<"NotTriangle\n"; else cout<<"Triangle\n"; } S2_2 #include usingnamespacestd; voidmain() { intyear; cout<<"Inputyear: "; cin>>year; if((year%4==0&&year%100! =0)||(year%400==0)) cout< else cout< } S2_3 #include usingnamespacestd; voidmain() { floata1,a2; charoper; floatresult; intflag=0; cout<<"请输入简单的运算式: \n"; cin>>a1>>oper>>a2; switch(oper) { case'+': result=a1+a2; break; case'-': result=a1-a2; break; case'*': result=a1*a2; break; default: cout<<"输入错误的运算符! \n"; flag=1; } if(flag==0) cout< } S2_4 #include #include usingnamespacestd; voidmain() { floata,b,c,x1,x2; cout<<"请输入方程的3个系数abc\n"; cin>>a>>b>>c; x1=(-b+sqrt(b*b-4*a*c))/2*a; x2=(-b-sqrt(b*b-4*a*c))/2*a; if(b*b-4*a*c<0) cout<<"无实根! \n"; else cout<<"x1="< cout<<"x2="< } S3_1 #include usingnamespacestd; voidmain() { intx,y; for(x=1;x<=30;x++) for(y=1;y<=30;y++) if(x+y==30&&2*x+4*y==90) cout<<"鸡有"< } S3_2 #include voidmain() { inta0,a1,a2; intb; for(a2=1;a2<=9;a2++) for(a1=1;a1<=9;a1++) for(a0=1;a0<=9;a0++) { b=100*a2+10*a1+a0; if(b==a2*a2*a2+a1*a1*a1+a0*a0*a0) cout< } } S3_3 #include voidmain() { inta0,a1,a2; intb; for(b=100;b<=999;b++) { a2=b/100; a1=(b/10)%10; a0=b%10; if(b==a0*a0*a0+a1*a1*a1+a2*a2*a2) cout< } } S3_4 #include voidmain() { inta,b,c; for(a=1;a<=36;a++) for(b=1;b<=36;b++) for(c=1;c<=36;c++) if(a+b+c==36&&a*4+b*2+c/2==36&&c%2==0) cout<<"男人"< } S3_5_1 #include voidmain() { inta; for(a=1;a<=599;a++) { if((a%10==5||(a/10)%10==5||(a/10/10)%10==5)&&(a%3==0)) cout< } } S3_5_2 #include voidmain() { inta,b,c=0; cout<<"请输入整数: "; cin>>a; while(a) {b=a%10; c=c+b; a=a/10;} cout< } S4_1 #include voidmain() { inta,b; intx,t; cout<<"请输入两个正整数: "; cin>>a>>b; x=a*b; if(a { t=a;a=b;b=t;} while(t=a%b) { a=b; b=t; } cout<<"最大公约数: "< cout<<"最小公倍数: "< } S4_2 #include voidmain() { inta,b,c; cout<<"Inputthreenumber: "; cin>>a>>b>>c; for(inti=a;;i--) if(a%i==0&&b%i==0&&c%i==0) break; cout< } S4_3 #include #include voidmain() { inta; cout<<"请输入一个整数: "; cin>>a; for(inti=2;i if(a%i==0) break; if(a%i! =0) cout< else cout< } S4_4 #include voidmain() { inta,b,c,x; cout<<"Inputthreenumber: "; cin>>a>>b>>c; for(x=a;;x++) if(x%a==0&&x%b==0&&x%c==0) break; cout< } S4_5 #include voidmain() { inta; for(a=100;a<=200;a++) { for(intb=2;b if(a%b==0) break; if(a==b) cout< } } S4_6 #include usingnamespacestd; intmain() { intn,i,sum; for(n=2;n<1000;n++) { //p=n/2; sum=0; for(i=1;i { if(n%i==0) sum=sum+i; } if(sum==n) cout< } return0; } S5_1 #include voidmain() { inta=2,b=1,c=0; for(;b<=7;b++) {c+=a; a=a*10+2; } cout< } S5_2 #include voidmain() { inta=2,b=1,c=0; for(;b<=7;b++) {c+=a; a=a*10+2; } cout< } S5_3 #include #include doublex; intmain() { cout<<"Inputx: "; cin>>x; doubley1,y2=x; do{ y1=y2; y2=2.0/3.0*y1+x/(3.0*y1*y1); }while(fabs(y1-y2)>=1e-5); cout<<"y="< return0; } S5_4 #include voidmain() { floatx; cout<<"Inputx: "; cin>>x; intn=1; floatg=0; floatt=x; do{ g+=t; n++; t=-t*x*x/(2*n-1)/(2*n-2); }while(n<=10); cout< } S6_1 include doubleabc(doublex) { doubley; y=2*x*x+3*x+5; returny;} voidmain() { doublea,b; a=abc (1); b=abc(1.2); cout<<"a="< cout<<"b="< } S6_2 #include #include floatf(floatx) {floaty; y=x*x+3*x-4; returny;} floatdf(floatx) {floaty; y=2*x+3; returny; } voidmain() { floatx0,x1; x1=0; do{ x0=x1; x1=x0-f(x0)/df(x0);} while(fabs(f(x0))>=1e-5); cout<<"x="< } S6_3 #include #include floatf(floatx) {returnx*x+3*x-4;} voidmain() { floatx1,x2,x0; do{ cout<<"请输入初值x1和x2: "; cin>>x1>>x2;}while(f(x1)*f(x2)>0); do{ x0=(x1+x2)/2; if(f(x0)*f(x1)>0) x1=x0; else x2=x0;} while(fabs(f(x0))>=1e-5); cout<<"x="< } S6_4 #include #include floatf(floatx) {returnx*x+3*x-4;} voidmain() { floatx1,x2,x0; do{ cout<<"请输入初值x1和x2: "; cin>>x1>>x2;}while(f(x1)*f(x2)>0); do{ x0=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)); if(f(x0)*f(x1)>0) x1=x0; else x2=x0;} while(fabs(f(x0))>=1e-5); cout<<"x="< } S7_1 #include intprime(intx) { for(inti=2;i if(x%i==0) return0; return1; } voidmain() { for(intx=100;x<=200;x++) {if(prime(x)==1) cout< } } S7_2 #include intprime(intnum) {for(inti=2;i if(num%i==0) return0; return1; }voidmain() { intn; cout<<"请输入一个整数n: "; cin>>n; for(inti=2;i {if(n%i==0&&prime(i)==1) cout< } cout< } S7_3 #include intprime(inta) {for(inti=2;i if(a%i==0) return0; return1; } voidmain() {intn; do{cout<<"输入一个不小于6的偶数: "; cin>>n;} while(n%2! =0||n<6); for(inti=2;i {if(prime(i)==1&&prime(n-i)==1) cout< } cout< } S7_4 #include intfun(intx) {inta,b=1; while(x) {a=x%10; b=b*a; x=x/10;} returnb; } voidmain() { intn,mul; cout<<"请输入一个整数(输入0结束): "; cin>>n; while(n) {mul=fun(n); cout< cout<<"请输入一个整数(输入0结束): "; cin>>n; } } S8_1 #include voidmain() { inta[6]={9,8,7,6,5,4}; inti,j,t; intn=6; for(i=0;i for(j=0;j {if(a[j]>a[j+1]) {t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } for(i=0;i cout< cout< } S8_2 #include voidmain() { inta[6]={9,8,7,6,5,4}; inti,j,min,t; intn=6; for(j=0;j { min=j; for(i=j;i if(a[min]>a[i]) min=i; t=a[min]; a[min]=a[j]; a[j]=t; } for(i=0;i cout< cout< } S8_3 #include voidmain() { inta[10]={2,5,78,4,6,3,89,12,15,34}; inti,max; intn=10; max=0; for(i=0;i<10;i++) { if(a[i]>a[max]) max=i;} cout<<"max="< } S8_4 #include voidmain() { inta[5]={8,16,5,14,1}; inti,j,n=5; for(i=0;i {j=a[i]; a[i]=a[n-1-i]; a[n-1-i]=j;} for(i=0;i cout< } S9_1 #include voidmain() { inti,j,row=0,colum=0,max; inta[4][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8},{4,6,34,1}}; max=a[0][0]; for(i=0;i<4;i++) for(j=0;j<4;j++) if(a[i][j]>max) {max=a[i][j];row=i; colum=j; }cout<<"max="< } S9_2 #include voidmain() { inti,j,k; intcolum,saddle,max,flag=0; inta[4][4]={{5,12,23,56},{19,28,37,46},{-12,-34,-6,8},{4,6,34,10}}; for(i=0;i<4;i++) { max=a[i][0]; for(j=0;j<4;j++) if(a[i][j]>max) { max=a[i][j]; colum=j; } for(k=0;k<4;k++) if(a[k][colum] max=a[k][colum]; if(a[k][j] {flag=1; saddle=max; break; } } if(flag=1) cout<<"saddle=" < else cout<<"NoSaddle! \n"; } S9_3_1 #include voidmain() { inti,j,k,n=4; inta[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}; for(i=0;i for(j=i;j { k=a[i][j]; a[i][j]=a[j][i]; a[j][i]=k; } for(i=0;i {for(j=0;j {cout< cout< } S9_3_2 #include voidmain() { inta[9][9]; for(inti=1;i<=9;i++) for(intj=1;j<=i;j++) a[i-1][j-1]=i*j; for(intb=0;b<9;b++) { for(intc=0;c<=b;c++) cout< cout< } } S9_4 #include voidmain() { inti,j,a[10][10]={0}; a[0][0]=a[1][0]=a[1][1]=1; for(i=2;i<=9;i++) { a[i][0]=1; a[i][i]=1; for(j=1;j<=9;j++) a[i][j]=a[i-1][j-1]+a[i-1][j]; } for(i=0;i<=9;i++) for(j=0;j<=i;j++) {
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 程序设计 源代码 大全