欢迎来到天天文库
浏览记录
ID:6784631
大小:93.50 KB
页数:9页
时间:2018-01-25
《浙大 java语言程序设计编程答案6》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
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.);}}}23、.求1-1/3+1/5-1/7+……输入一个正整数repeat(04、tri,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;}5、System.out.println((int)(sum*10000+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、917)32.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;//分母b7、=2;//分子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、2+222)98760(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、tri,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;}5、System.out.println((int)(sum*10000+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、917)32.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;//分母b7、=2;//分子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、2+222)98760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
4、tri,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;}
5、System.out.println((int)(sum*10000+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、917)32.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;//分母b7、=2;//分子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、2+222)98760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
6、917)32.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
7、=2;//分子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、2+222)98760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
8、2+222)98760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
此文档下载收益归作者所有