说明学习物理chapterhcs_第1页
说明学习物理chapterhcs_第2页
说明学习物理chapterhcs_第3页
说明学习物理chapterhcs_第4页
说明学习物理chapterhcs_第5页
已阅读5页,还剩66页未读 继续免费阅读

付费下载

下载本文档

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

文档简介

Maingotowhiledo-whileforNestingofComparisonofthebreakandcontinue casestudy(ExhaustivealgorithmandI tivealgorithm) Problem6.1What’sloo ?Whywouldweuse 6.1What’sloo ?Whywouldweuse WhenWhenpisWhenpisLoop while pre- while(test{bodyofthe} Flow shouldbe.whenconditionis.

Controlvariableshouldbeupdatedinthebodyloop. example,calculatevoidmain({intn=2;floatsum=1;while(n<=10){}

/*nisthecontrolvariable/*n++;updatethecontrolprintf("sum=%f\n",printf("sum=%f\n",} voidmain({intn=2;floatsum=1;while(n<=10){sum=sum*n;}}Infinite nothing

voidmain({intn=2;floatsum=1;while(n++<=10){}}≠ ExerciseinWriteacprogramtoprintoutalltheprimenumbersfrom2to200. do-whiledbodybodyofthe}while(testcondition) differentwithwhile,inthedo-whileconstruct,thebodyoftheloopisalwaysexecutedatleastonce.FlowWhenpis Ifthebodyoftheloopwilldointhefirsttimewithoutanycondition,usedo-while.endwithasemicolon(;).Soweendwithasemicolon(;).Sowecan“do-while”asonethelooptoTheremustexistastatementwhichtheloopto Calculate10!withdo-voidmain(floatsum=1;do{floatsum=1;do{}while(n<=10);printf("sum=%f\n",sum);} voidmain({inti=10;while(i<10);}

voidmain({intx=23;printf("%d\n",x--);}

voidmain({intx=3;printf("x=%d\n",x);}

voidmain({inti=1;while(i++<8);}}

Inputacharacterstringwhichends‘?’,counthowmanydigitsarein inputchprintcountif(ch>=’0’&&ch<=’9printcount voidmain(){intcount=0;charch;ch=getchar();while(ch!=’?’){if(ch>=’0’&&ch<=’9’)count++;ch=getchar();} while((ch=getchar())!=’?’)if(ch>=’0’&&ch<=’9’)count++; for{bodyoftheexpression1:initializing 达式orrepetition达式orrepetitionexpression3:controlvariableFlow}bodyofthe{bodyoftheCalculatevoidmain({intfloatn<=100{intfloatn<=100is} expression1,expression2andexpression3can expression1,expression2andexpression3canomitted,but“;”should。Itmeansnobodyofloopwhenfor(;;)endwith“;”floatfloatfor(sum=1,n=2;n<=100;sum*=n,; intn=2;floatsum=1;for(;n<=100;)intn=2;floatsum=1;for(;;)elsebreak;

式1、式2、式3都可省, Input50digitsof0~9,counthowmanyevens,oddsandzerosare, Theyareacceptedbynumericalcharacters,notnumbers.characteristransferredintowholenumber.Wecancharacteristransferredintowholenumber. voidmain({inti,n_even=0,n_odd=0,n_zero=0;charnum;for(i=1;i<=50;i++)scanf("%c",&num);num=num-’0’;if(num!=0&&num%2==0)n_even++;if(num%2!=0)n_odd++;if(num==0)}printf("Thereare%2devens,%2dodds,%2dzeros",n_even,n_odd,n_zero);}

thereare9evens,8zeros,19nestingofOneloopstatementwithinanotherloopUsingwhile、do-whileandforcanbeincludedeachUsing while(while(}while(

for(;;for(;;}while(while();} for(;;while({……}{…for(;;}while( Nestingof2x1=23x1=33x2=64x1=42x1=23x1=33x2=64x1=44x2=84x3=125x1=55x2=105x3=155x4=206x1=66x2=126x3=186x4=246x5=307x1=77x2=147x3=217x4=287x5=357x6=429x1=99x2=189x3=279x4=369x5=459x6=549x7=639x8=729x1=99x2=189x3=279x4=369x5=459x6=549x7=639x8=72 S1:S1:for(i=1;i<=9;{printtheithrowS1.1:for(j=1;j<=i;

Nestingof{printthejthcolumnS1.1.1:printi*jontheithrowandjthcolumnprintf("%d*%d=%d",i,j,i*j); Nestingofvoidmain({for(i=1;i<10;i++)for(i=1;i<10;i++)for(j=1;j<=i;j++)printf("%dx%d=%d",i,j,i*j);}}} Nestingof while(i<=8)for(k=1;k<=8-for(j=1;j<=(2*i-}}

voidmain({inti,j,k;while(i<=8)forfor(j=1;j<=(2*i-}} ifand breakandbreakWhenabreakstatementisencounteredinsidealoop,theloopisimmediayexitedandtheprogramcontinueswiththestatementimmediayfollowingtheloop.Inthecaseofnestingloop,breakonlyterminatesthecurrentloop. continuecontinuewillgoto“while”totesttheThecaseloop,continuewillgotoexpression3,thentestexpression2toSkipthefollowingstatementsandcontinuewiththenext whetheranotherloopwill nwhiley

whileyif(exp2)

if(exp2)

statements statements Writeaprogramtocomputes=1+1/2+1/3+…+1/n+… voidmain({longfloatmax,s=0.0;scanf("%f",&max);for(n=1;;n++){

/*usebreakto}printf("s=%f,n=%d\n",s,}

s=10.000051, inputExpression2isinputExpression2isalways Breakwillprints,for(n=1;;n++){ }printf("%f,%d\n",s, 6.96.9case Testallcases(orstates)untiloneormuchmoresolutionsisgot.Controlledloopexactlyhowmanytimestheloopexactlyhowmanytimestheloopwillbeexecuted。Sentinel-controlledloops:thenumberofrepetitionsisnotknownbeforetheloop Movebricksmoves4bricksonetime,3perwomanonetimeand1brickfortwochildsonetime.Nowmoves4bricksonetime,3perwomanonetimeand1brickfortwochildsonetime.Nowrequireallthebricksaremovedinonetime,howmanymanandwomenandchildsareneeded?Wecan S1:because0<=men<9,wecanS1:because0<=men<9,wecangetwomenchild}while(while(women<12{findout} S3:child=36-men-womencondition: {intmen,women,child;for(men=0;men<9;men++){intmen,women,child;for(men=0;men<9;men++)for(women=0;women<12;women++){if(child%2==0&&}} voidmain({intmen,women,if(child%2==0&&}}}} tive tionistouseanewvaluetore theoldoneorusetheoldvaluetodeduceanewonewithrecursivemethodTwo Exacti tion:Theexactsolutionwillbe gotafterseverali Approximatei tion:Onlytheapproximatesolutionisgotafterseveral tion.Itcan’tfindexactsolution,butcanapproachtoit.Useaccuracytocontroltherepetitiontimes. Thereare1.4billionpeoplenow.Howmuchisitafter10yearsiftheannualgrowthrateis2%.Supposethepresentpeopleism,1yearlater,itwillbe:m=m*(1+2%),Wecanget10yearslater’snumberbyexecuting10timesm=m*(1+2%)。initial tive tivetimes:for(i=1;i<11;i++)m=m*(1.+2./100.);

Example2,Printoutthefirst40itemsoftheFibonacciseries. Suchas:Initialvalue:fib1=1; tive tivetimes:40-

Initialvalue:fib1=1; tiveformula tivetimesvoidmain({intlongfib,fib1=1,

voidmain({intprintf("%ld%ld",fib1,fib2);longfib1=1,for(m=3;m<=40;m++){printf("%12ld",fib);}}

for(m=1;m<=20;m++)printf("%12ld%12ld",fib1,fib2);if(m%2==0)printf("\n");}} InitialExample3,useInitial tiveErroris=1.0E-6,whichdecidestheconditionif tionwillcontinue.while(fabs(term)>),i onceagain. Example4,Howtosolveasimplenonlinearequationf(x)=0。Rewritethenonlinearequationf(x)=0asthei Giveafittingvaluex0,itcancreateaseries:x0,x1,x2,…,xn,…Iftheseriesconverge(i.e.thereexistsalimitation.),andthefunctioniscontinuousinthesolutioninterval,then,thelimitvalueistheapproximatesolutionoff(x)=0. Typic

温馨提示

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

评论

0/150

提交评论