资源描述
习题4答案1 #include iostream.hvoid main()double a,b,c,max;cout请输入三个数:abc;if(ab) max=a;else max=b;if(maxc) max=c;cout最大数为:maxendl;2#include iostream.h void main( ) int year;coutyear;if(year%4=0 & year%100!=0) | year%400=0)coutyear是闰年endl;elsecoutyear不是闰年=1000n+1nsum+nsum结束YN34 #include iostream.hvoid main()int i,count=0;for(i=1;i=1000;i+) if(i%9=0 & i%11=0)coutit;count+;if(count%5=0) coutendl;5If-else语句答案: #include iostream.hvoid main()int score;coutscore;if(score100 | score0)cout输入的成绩有错!endl;elsecout=90) coutA=80) coutB=70) coutC=60) coutDendl;else coutEendl;Switch语句答案:#include iostream.hvoid main()int score;coutscore;if(score100 | score0)cout输入的成绩有错!endl;elsecoutn五分制成绩为:;switch(score/10) case 10:case 9: coutAendl; break;case 8: coutBendl; break;case 7: coutCendl; break;case 6: coutDendl; break;default: coutEendl;6If-else语句答案:#include iostream.hvoid main()int base1,base2,base3,base4,base5,base6,base7,base8;float money,tax;base1=0.05*500;base2=base1+0.1*1500;base3=base2+0.15*3000;base4=base3+0.2*15000;base5=base4+0.25*20000;base6=base5+0.3*20000;base7=base6+0.35*20000;base8=base7+0.4*20000;coutmoney;if(money0)cout输入的金额不能是负数!endl;else money=money-800;if(money=0) tax=0;else if(money=500) tax=money*0.05;else if(money=2000) tax=base1+(money-500)*0.1; else if(money=5000) tax=base2+(money-2000)*0.15; else if(money=20000) tax=base3+(money-5000)*0.2; else if(money=40000) tax=base4+(money-20000)*0.25;else if(money=60000) tax=base5+(money-40000)*0.3; else if(money=80000) tax=base6+(money-60000)*0.35;else if(money=100000) tax=base7+(money-80000)*0.4; else tax=base8+(money-100000)*0.45;coutn应缴税款为:taxendl;7 #include iostream.hvoid main()int i,a,b,c,d;for(i=1000;i10000;i+)a=i/1000; /千位b=i%1000/100; /百位c=i%100/10; /十位d=i%10; /个位if(a*a*a*a+b*b*b*b+c*c*c*c+d*d*d*d=i)coutit;coutendl;8 #include iostream.h#include math.hvoid main()int i,j,m;for(i=2;i1000;i+)m=sqrt(i);for(j=2;j=m+1)coutit;cout0;j-,i-=2) for(m=0;mbeginCol+4-j;m+)cout ;for(m=0;mi;m+)cout*;coutendl;for(i=3,j=2;j=4;j+,i+=2) for(m=0;mbeginCol+4-j;m+)cout ;for(m=0;mi;m+)cout*;coutendl;coutendl; 10 #include iostream.hvoid main( ) int i, x, y, z;for(x=1;x=9;x+) for(y=0;y=9;y+) for(z=0;z=9;z+) if(x!=y & y!=z & z!=x & (x*100+y*10+z)%11=0) cout x*100+y*10+z t;coutendl;11#include iostream.hvoid main( ) int i; float m,n,sum=0; for(i=0,m=1,n=2; i30; i+) sum += n/m; n=n+m; m=n-m; coutsumendl;12#include iostream.hvoid main( ) int m,n=0;cout请输入一个正整数N(1=Nm;for(int i=1;i=m;i+)for(int j=1;j=m/2;j+)for(int k=1;k=m/5;k+)if(i*1+j*2+k*5=m)cout1元i个,2元j个,5元k个n;n+;cout总的组合数是:nendl;13#include iostream.hvoid main( ) int m=1;for(int i=1;i=50;i+)if(i%3=0)m *= i;cout3的倍数之积为:mendl;14#include iostream.hvoid main( ) char str100;int s=0;cin.getline(str,100);for(int i=0;stri!=0;i+)if(stri=c)s+;cout字母c出现的个数:sendl;15. #include iostream.hvoid main( ) int a,n,sum=0,t;coutan;t=a;sum=t;for(int i=2;i=n;i+)t = t*10+a;sum += t;coutsum=sumendl;16. #include iostream.hvoid main( ) int s1,s2,s3,s4,s5;char str100;cout=A & stri=a & stri=0 & stri=9) s3+;else if(stri= ) s4+;else s5+;cout大写英文字母:s1endl;cout小写英文字母:s2endl;cout数字字符:s3endl;cout空格:s4endl;cout其他字符:s5endl;17. #include iostream.h#include void main( ) double x1=1.0,x2,a;couta;if(a0)cout1e-6)x1=x2;x2=(x1+a/x1)/2;cout平方根:x2endl;
展开阅读全文