欢迎来到天天文库
浏览记录
ID:6179471
大小:93.50 KB
页数:9页
时间:2018-01-05
《浙大 java语言程序设计编程答案6》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验7循环结构程序的设计(二)程序填空,不要改变与输入输出有关的语句。1.求1-1/2+1/3-1/4+……输入一个正整数repeat(02、tring[]args){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*13、0000+0.5)/10000.);}}}2.求1-1/3+1/5-1/7+……输入一个正整数repeat(04、icstaticvoidmain(String[]args){intri,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+=5、item;flag=-flag;temp+=2;item=flag*1.0/temp;}System.out.println((int)(sum*10000+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、例:括号内是说明输入3(repeat=3)1520输出2.0(第1项是2.0)8.3917(前5项的和是8.3917)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=17、;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=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、和n,求a+aa+aaa+aa…a(n个a)之和。例:括号内是说明输入2(repeat=2)23(a=2,n=3)85(a=8,n=5)输出246(2+22+222)98760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
2、tring[]args){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*1
3、0000+0.5)/10000.);}}}2.求1-1/3+1/5-1/7+……输入一个正整数repeat(04、icstaticvoidmain(String[]args){intri,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+=5、item;flag=-flag;temp+=2;item=flag*1.0/temp;}System.out.println((int)(sum*10000+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、例:括号内是说明输入3(repeat=3)1520输出2.0(第1项是2.0)8.3917(前5项的和是8.3917)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=17、;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=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、和n,求a+aa+aaa+aa…a(n个a)之和。例:括号内是说明输入2(repeat=2)23(a=2,n=3)85(a=8,n=5)输出246(2+22+222)98760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
4、icstaticvoidmain(String[]args){intri,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+=
5、item;flag=-flag;temp+=2;item=flag*1.0/temp;}System.out.println((int)(sum*10000+0.5)/10000.);}}}1.求2/1+3/2+5/3+8/5+...输入一个正整数repeat(06、例:括号内是说明输入3(repeat=3)1520输出2.0(第1项是2.0)8.3917(前5项的和是8.3917)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=17、;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=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、和n,求a+aa+aaa+aa…a(n个a)之和。例:括号内是说明输入2(repeat=2)23(a=2,n=3)85(a=8,n=5)输出246(2+22+222)98760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
6、例:括号内是说明输入3(repeat=3)1520输出2.0(第1项是2.0)8.3917(前5项的和是8.3917)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
7、;ri<=repeat;ri++){n=in.nextInt();/*--------------------*/a=1;//分母b=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、和n,求a+aa+aaa+aa…a(n个a)之和。例:括号内是说明输入2(repeat=2)23(a=2,n=3)85(a=8,n=5)输出246(2+22+222)98760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
8、和n,求a+aa+aaa+aa…a(n个a)之和。例:括号内是说明输入2(repeat=2)23(a=2,n=3)85(a=8,n=5)输出246(2+22+222)98760(8+88+888+8888+88888)importjava.util.Scanner;publicclassTest40012{publicstaticvoidmain(Stringargs[]){intri,repeat;inti,n,a,sn,t
此文档下载收益归作者所有