(4.7)--4.7Nestediterationanditsimplemen.pdf

上传人:刘静 文档编号:57974474 上传时间:2022-11-06 格式:PDF 页数:8 大小:616.03KB
返回 下载 相关 举报
(4.7)--4.7Nestediterationanditsimplemen.pdf_第1页
第1页 / 共8页
(4.7)--4.7Nestediterationanditsimplemen.pdf_第2页
第2页 / 共8页
点击查看更多>>
资源描述

《(4.7)--4.7Nestediterationanditsimplemen.pdf》由会员分享,可在线阅读,更多相关《(4.7)--4.7Nestediterationanditsimplemen.pdf(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Nested iterationand its implementationNested iteration and its implementation4.7If the processing inside an iterative algorithm is still iterativeitself,thenitformsanestediterationstructure.Inpracticalapplications,nested iteration is often applied in C+programs,whichmeans there could be various loop

2、 statements in the body of a loop andforms a nested iterative structure,which is usually called multipleloops.Nested iteration and its implementation4.7In this problem,we need to deal with an n-term accumulation,which means it needs to be repeated for n times.For each term inthe accumulation,it requ

3、ires an iterative processing,i.e.tocalculate the i-th term,the operation of multiplying by 2 isrepeated for i times.Therefore,this algorithm requires a nestediterative structure.The nested iterative algorithm for this problemis shown in the following table.Problem solving using computational thinkin

4、g:Eg1Design an algorithm to calculate 21+22+23+2n,and implement it in a C+program.Nested iteration and its implementation4.7StepProcessing1Input n,the highest power of 22Initialize sum to 0 for each term in the accumulation3Set the range of i to 1n,and operate according to the following steps:Set po

5、wer,which is used to store the result for the i-th term,to 1Repeat the following steps for i times:power=power2Add power to sum4Output the results,sumNested iteration and its implementation4.7int main()int n,sum,power,i,j;coutn;sum=0;for(i=1;i=n;i+)power=1;j=1;while(j=i)power=power*2;j+;sum=sum+powe

6、r;coutThe sum of 2 to the 1st power to 2 to the n th power is:sumendl;return 0;Question:How to implement the inner loop using for statement?Nested iteration and its implementation4.7Tips:Iterative statements of different structures can also form a nested iteration when theyre combined together,for e

7、xample,while loop can be nested in for loop.The execution order of nested iteration is that when the outer loop is executed,and encounters the inner loop,it will first complete the execution of all inner loops,and then continue to execute the next outer loop.Multiple inner loops can be nested in the

8、 body of a loop.Nested iteration and its implementation4.7Since the Narcissistic numbers are three digit integers,their valuemust be in the range of 100999.Using the triple loop,the outerloop variable i controls the value of hundreds digit varies from 1 to9,the middle loop variable j controls the va

9、lue of tens digit variesfrom 0 to 9,the inner loop variable k controls the the value of onesdigit varies from 0 to 9,exhaustively search and determine whethereach one of all three digit integers is an Narcissistic number.Problem solving using computational thinking:Eg2Narcissistic number(or Shui xia

10、n hua number in Chinese)refers to a three digitinteger whose sum of the cubes of each digit is equal to the number itself.For example,since 153=13+53+33,153 is a narcissistic number.Write a program to find out all Narcissistic numbers.Nested iteration and its implementation4.7int main()int i,j,k,m,n

11、;for(i=1;i=9;i+)/outer loop,controls the hundreds digitfor(j=0;j=9;j+)/middle loop,controls the tens digitfor(k=0;k=9;k+)/inner loop,controls the ones digitm=i*i*i+j*j*j+k*k*k;n=100*i+10*j+k;if(m=n)coutmendl;return 0;Question:How to find out only thefirst Narcissistic numberwith the program shownon the left?

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 大学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁