1.循环次数已知的累加和问题习题4.10:编程计算1+3+5+7+……+99+101的值。/*方法一:使用for语句实现1。*/#includestdio.hmain(){inti;intsum=0;//累加和变量清零for(i=1;i=51;i++){sum=sum+(2*i-1);}printf(1+3+5+……+101=%d\n,sum);}/*方法二:使用for语句实现2。*/main(){inti;intsum=0;for(i=1;i=101;i=i+2){sum=sum+i;}printf(1+3+5+……+101=%d\n,sum);}/*方法三:使用while语句实现。*/main(){inti;intsum=0;i=1;//循环变量初始化while(i=101){sum=sum+i;i=i+2;}printf(1+3+5+……+101=%d\n,sum);}/*方法四:使用do-while语句实现。*/main(){inti;intsum=0;i=1;do{sum=sum+i;i=i+2;}while(i=101);//注意末尾的分号printf(1+3+5+……+101=%d\n,sum);}习题4.11:编程计算1*2*3+3*4*5+……+99*100*101的值。#includestdio.hmain(){inti;longintsum=0;/*注意变量的类型定义*/longintterm;for(i=1;i=99;i=i+2){term=i*(i+1)*(i+2);sum=sum+term;}printf(1*2*3+3*4*5+……+99*100*101=%ld\n,sum);}习题4.12:编程计算1!+2!+3!+……+10!的值。#includestdio.hmain(){inti;longterm=1;//等价于longintterm=1;longsum=0;for(i=1;i=10;i++){term=term*i;sum=sum+term;}printf(1!+2!+3!+……+10!=%ld\n,sum);}习题4.13:编程计算a+aa+aaa+……+aa……a(n个a)的值,n和a的值由键盘输入。main(){inti,n;inta;longintterm=0,sum=0;printf(pleaseinputa:\n);scanf(%d,&a);printf(pleaseinputn:\n);scanf(%d,&n);for(i=1;i=n;i++){term=term*10+a;if(in)//=====此处加入if语句,可以控制输出项的形式=====printf(%ld+,term);//========前面的项=======elseprintf(%ld=,term);//========最后一项=======sum=sum+term;}printf(%ld\n,sum);}2.循环次数已知的累乘积问题习题4.14:main(){doublei;doublepi,p=1.0,term;//注意变量的类型for(i=1;i=50;i++){term=(2*i*2*i)/((2*i-1)*(2*i+1));p=p*term;}pi=p*2;printf(pi=%f\n,pi);}3.循环次数未知的累加和问题习题4.15:#includestdio.h#includemath.hmain(){intn=1,count=0;doublee=0.0,term=1.0;while(fabs(term)=1e-5){//为了使最后一项的绝对值达到精度要求,要求累加项满足大于等于1e-5e=e+term;//将累加项累加到和中count++;//计数器加1term=term/n;//计算累加项n=n+1;//累加项的分母变化}printf(1+1/1!+1/2!+……+1/n!=%f\n,e);}习题4.16:#includestdio.h#includemath.hmain(){intn=1;doublesum=0.0,term,sign=1.0;do{term=sign/n;sum=sum+term;sign=-sign;n=n+1;}while(fabs(term)=1e-4);printf(sum=%f\n,sum);}习题4.17:#includestdio.h#includemath.hmain(){intn=1,count=1;doublex,term,sum;printf(pleaseinputx:\n);scanf(%lf,&x);term=x;sum=x;do{term=-term*x*x/((n+1)*(n+2));sum=sum+term;count++;n=n+2;}while(fabs(term)=1e-5);printf(sinx=%lf\n,sum);printf(共累加了%d项!\n,count);}4.循环结构与选择结构的综合应用问题习题4.18:打印所有的“水仙花数”,所谓“水仙花数”是指一个三位数,各位数字的立方等于该数本身。#includestdio.hmain(){inti,j,k,n;for(n=100;n=999;n++){i=n/100;//分离百位数字j=n%100/10;//分离十位数字k=n%10;//分离个位数字if(n==i*i*i+j*j*j+k*k*k){printf(%d\t,n);//打印水仙花数}}}5.循环的嵌套解决穷举问题习题4.25:用1元5角钱人民币兑换5分、2分和1分的硬币(每一种都要有)共100枚,问共有几种兑换方案?每种方案兑换多少枚?#includestdio.hmain(){intx,y,z;intcount=0;printf(5fen\t2fen\t1fen\n);for(x=1;x=29;x++){for(y=1;y=72;y++){z=100-x-y;if(5*x+2*y+z==150){printf(%d\t%d\t%d\n,x,y,z);count++;}}}printf(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n);printf(1元5角钱人民币兑换成5分、2分、1分的硬币,共有%d中兑换方案!\n,count);printf(++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n);}习题4.22:三色球问题:若一个口袋中放有12个球,其中有3个红色的,3个白色的,6个黑色的,从中任取8个球,问共有多少种不同的颜色搭配?#includestdio.hmain(){intred,white,black;printf(red\twhite\tblack\n);for(red=0;red=3;red++){for(white=0;white=3;white++){black=8-red-white;if(black=0&&black=6){printf(%3d\t%3d\t%3d\n,red,white,black);}}}}习题4.23:鸡兔同笼:共有98个头,386只脚,编程求鸡、兔各多少只?#includestdio.hmain(){intx,y;printf(chick\trabit\t\n);for(x=1;x=97;x++){y=98-x;if(2*x+4*y==386){printf(%d\t%d\n,x,y);}}}习题4.24:百鸡问题:公鸡每只5元,母鸡每只3元,小鸡3只1元。用100元买100只鸡,问公鸡、母鸡和小鸡各能买多少只?#includestdio.hmain(){intx,y,z;printf(公鸡\t母鸡\t小鸡\n);for(x=0;x=20;x++){for(y=0;y=20;y++){z=100-x-y;if(5*x+3*y+z/3==100){printf(%d\t%d\t%d\n,x,y,z);}}}}6.循环的嵌套打印图形打印图形#includestdio.hmain(){inti,j;for(i=1;i=4;i++){for(j=1;j=4-i;j++){printf();}for(j=1;j=2*i-1;j++){printf(*);}printf(\n);}for(i=3;i=1;i--){for(j=1;j=4-i;j++){printf();}for(j=1;j=2*i-1;j++){printf(*);}printf(\n);}}打印空心菱形#includestdio.hmain(){inti,j,k;for(i=1;i=4;i++){for(j=1;j=4-i;j++){printf();}for(j=1;j=2*i-1;j++){if(j==1||j==2*i-1){printf(*);}else{printf();}}printf(\n);}for(i=1;i=3;i++){for(j=1;j=i;j++){printf();}for(j=1;j=7-2*i;j++){if(j==1||j==7-2*i){printf(*);}else{printf();}}printf(\n);}}打印圣诞树#includestdio.hmain(){inti,j;for(i=1;i=4;i++){for(j=1;j=5-i;j++){printf();}for(j=1;j=2*i-1;j++){printf(*);}printf(\n);}for(i=1;i=5;i++){for(j=1;j=5-i;j++){printf();}for(j=1;j=2*i-1;j++){printf(*);}printf(\n);}for(i=1;i=3;i++){for(j=1;j=4;j++){printf();}printf(*);printf(\n);}}打印九九乘法表。#includestdio.hmain(){inti,j;for(i=1;i=9;i++){//控制行for(j=1;j=i;j++){//控制列printf(%d*%d=%-3d,j,i,i*j);//%-3d控制整型表达式i*j的输出占3列,左对齐}printf(\n);}}#includestdio.hmain(){inti,j;for(i=1;i=9;i++){for(j=1;j=9;j++){if(i=j){printf(%d*%d=%-3d,i,j,i*j);}}printf(\n);}}7.break语句和continue语句的应用习题4.21:爱因斯坦数学题。(使用break语句)#includestdio.hmain(){intx;//阶梯数for(x=1;;x++){if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0){break;}}printf(共有%d阶台阶。\n,x);}习题4.18:打印所有“水仙花数”。(使用continue语句)#includestdio.hmain(){inti,j,k,n;for(n=100;n=999;n++){i=n/100;//分离百位数字j=n%100/10;//分离十位数字k=n%10;//分离个位数字if(n!=i*i*i+j*j*j+k*k*k){continue;//continue语句跳出本次循环}printf(%d\t,n);//打印水仙花数}}输出100以内的所有素数,要求每行输出5个素数。(使用break语句)#includestdio.h#includemath.hmain(){intnumbe