欢迎来到天天文库
浏览记录
ID:51242318
大小:52.50 KB
页数:9页
时间:2020-03-10
《浙大Java语言程序设计编程答案6.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验7循环结构程序的设计(二)程序填空,不要改变与输入输出有关的语句。1.求1-1/2+1/3-1/4+……输入一个正整数repeat(02、{intri,repeat;inti,n,flag;floatsum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/sum=0;flag=1;for(i=1;i<=n;i++){sum+=flag*1.0f/i;flag=-flag;}System.out.println((long)(sum*10000+0.5)/10000.);}}}2.求3、1-1/3+1/5-1/7+……输入一个正整数repeat(04、,repeat;inttemp,flag;doubleeps,item,sum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){eps=in.nextDouble();/*--------------------*/item=1;sum=0;temp=flag=1;while(Math.abs(item)>=eps){sum+=item;flag=-flag;temp+=2;item=flag*1.0/temp;}Syst5、em.out.println((int)(sum*10000+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、2.6603(前20项的和是32.6603)importjava.util.Scanner;publicclassTest40011{publicstaticvoidmain(String[]args){intri,repeat;inti,n;floata,b,s,t;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=2;//分7、子s=0;for(i=1;i<=n;i++){s+=b/a;t=a;a=b;//分母是前一项的分子b=t+b;//分子是前一项分子与分母的和}System.out.println((int)(s*10000+.5)/10000.);}}}1.求a+aa+aaa+aa…a输入一个正整数repeat(08、8760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
2、{intri,repeat;inti,n,flag;floatsum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/sum=0;flag=1;for(i=1;i<=n;i++){sum+=flag*1.0f/i;flag=-flag;}System.out.println((long)(sum*10000+0.5)/10000.);}}}2.求
3、1-1/3+1/5-1/7+……输入一个正整数repeat(04、,repeat;inttemp,flag;doubleeps,item,sum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){eps=in.nextDouble();/*--------------------*/item=1;sum=0;temp=flag=1;while(Math.abs(item)>=eps){sum+=item;flag=-flag;temp+=2;item=flag*1.0/temp;}Syst5、em.out.println((int)(sum*10000+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、2.6603(前20项的和是32.6603)importjava.util.Scanner;publicclassTest40011{publicstaticvoidmain(String[]args){intri,repeat;inti,n;floata,b,s,t;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=2;//分7、子s=0;for(i=1;i<=n;i++){s+=b/a;t=a;a=b;//分母是前一项的分子b=t+b;//分子是前一项分子与分母的和}System.out.println((int)(s*10000+.5)/10000.);}}}1.求a+aa+aaa+aa…a输入一个正整数repeat(08、8760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
4、,repeat;inttemp,flag;doubleeps,item,sum;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){eps=in.nextDouble();/*--------------------*/item=1;sum=0;temp=flag=1;while(Math.abs(item)>=eps){sum+=item;flag=-flag;temp+=2;item=flag*1.0/temp;}Syst
5、em.out.println((int)(sum*10000+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、2.6603(前20项的和是32.6603)importjava.util.Scanner;publicclassTest40011{publicstaticvoidmain(String[]args){intri,repeat;inti,n;floata,b,s,t;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=2;//分7、子s=0;for(i=1;i<=n;i++){s+=b/a;t=a;a=b;//分母是前一项的分子b=t+b;//分子是前一项分子与分母的和}System.out.println((int)(s*10000+.5)/10000.);}}}1.求a+aa+aaa+aa…a输入一个正整数repeat(08、8760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
6、2.6603(前20项的和是32.6603)importjava.util.Scanner;publicclassTest40011{publicstaticvoidmain(String[]args){intri,repeat;inti,n;floata,b,s,t;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=2;//分
7、子s=0;for(i=1;i<=n;i++){s+=b/a;t=a;a=b;//分母是前一项的分子b=t+b;//分子是前一项分子与分母的和}System.out.println((int)(s*10000+.5)/10000.);}}}1.求a+aa+aaa+aa…a输入一个正整数repeat(08、8760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
8、8760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
此文档下载收益归作者所有