浙大JAVA 实验题答案
浙大JAVA实验题答案
实验8 Method的使用1.程序填空题,不要改变与输入输出有关的语句;50001输入一个正整数repeat 0<repeat<10,做repeat次下列运算:输入1 个正整数n,计算 s 的前n项的和保留 4 位小数;s = 1 + 1/2 +....+ 1/n要求定义并调用函数factn计算n的阶乘;例:括号内是说明输入:2 repeat=22 n=210 n=10输出:public class Test50001 {public static void mainString args {int ri,repeat;int i,n;double s;Scanner in=new Scanner;repeat=;forri=1;ri<=repeat;ri++{n=;/-----------/s=0;for i=1;i<=n;i++s+=fact i;}}/---------------/static double fact int n {int i;double f=1;for i=1;i<=n;i++f=i;return f;}}50002输入一个正整数repeat 0<repeat<10,做repeat次下列运算:输入2个正整数a和n, 求a+aa+aaa+aa…an个a之和;要求定义并调用函数fna,n,它的功能是返回aa…an个a;例如,fn3,2的返回值是33;例:括号内是说明输入2 repeat=22 3 a=2, n=38 5 a=8, n=5输出246 2+22+22298760 8+88+888+8888+88888imponner;public class Test50002{public static void mainString args{int ri, repeat;int i, n,a;long sn;Scanner in=new Scanner;repeat=;forri=1; ri<=repeat; ri++{a=;n=;/------------/sn=0;for i=1;i<=n;i++sn+=fn a,i;}}/------------/s tatic int fn int a,int n{int s=0;forint i=1;i<=n;i++s=s10+a;return s;}}50003输入一个正整数repeat 0<repeat<10,做repeat次下列运算:读入1 个整数,统计并输出该数中2的个数;要求定义并调用函数countdigitnumber,digit,它的功能是统计整数number中数字digit的个数;例如,countdigit10090,0的返回值是3;例:括号内是说明输入:3 repeat=3-219022345543输出:count=2 -21902中有2个2count=1 有1个2count=0 345543中没有2public class Test50003{public static void mainString args{int ri, repeat;int count;long n;Scanner in=new Scanner;repeat=;forri=1; ri<=repeat; ri++{n=;/---------/n=n;count=countdigit n,2;}}/------------/static int countdigit long number,int digit{....要求定义并调用函数fibn,它的功能是返回第n项Fibonacci数;例如,fib7的返回值是13;例:括号内是说明输入:3 repeat=31 10 m=1, n=1020 100 m=20, n=1001000 6000 m=1000, n=6000输出:1 123 5 8 1到10之间的Fibonacci数21 34 55 89 20到100之间的Fibonacci数1597 2584 4181 1000到6000之间的Fibonacci数public class Test50006{public static void mainString args{int ri,repeat;int i, m, n;long f;Scanner in=new Scanner;repeat=;forri=1; ri<=repeat; ri++{m=;n=;/---------/i=1;f=1;while f<=n{if f>=m " ";i++;f=fib i;}}}/------------/sta ti c long fib int n{ //返回第n项Fibonacci数int i;long a=1,b=1,f=1;for i=3;i<=n;i++{ //从第3项开始计算f=a+b;a=b;b=f;}return f;}}50007输入一个正整数repeat 0<repeat<10,做repeat次下列运算:输入2 个正整数m和n1<=m,n<=10000,输出m 到n之间的所有完数完数就是因子和与它本身相等的数;要求定义并调用函数factorsumnumber,它的功能是返回number的因子和;例如,factorsum12的返回值是161+2+3+4+6;例:括号内是说明输入:2 repeat=220 500 m=100, n=4001 100 m=1, n=100输出:28 4961 6 28public class Test50007{public static void mainString args{int ri,repeat;int i, m, n;Scanner in=new Scanner;repeat=;forri=1;ri<=repeat;ri++{m=;n=;/---------/for i=m;i<=n;i++if i==factorsum i" ";}}/---------/static int factorsum int number{ //返回number的因子和int sum=0;if number==1sum=1;forint i=1;i<=number-1;i++if number%i==0sum+=i;return sum;}}50008输入一个正整数repeat 0<repeat<10,做repeat次下列运算:输入2 个正整数m和n1<=m,n<=1000,输出m 到n之间的所有满足各位数字的立方和等于它本身的数;要求定义并调用函数isnumber判断number的各位数字之立方和是否等于它本身;例:括号内是说明输入:2 repeat=2100 400 m=100, n=4001 100 m=1, n=100输出:153 370 371 111+555+333=153; 333+777=370; 333+777+111=3711public class Test50008{public static void mainString args{int ri,repeat;int i, m, n;Scanner in=new Scanner;repeat=;forri=1;ri<=repeat;ri++{m=;n=;/---------/for i=m;i<=n;i++if is i" ";}}/---------///判断number的各位数字之立方和是否等于它本身static boolean is int number{int sum=0,n,digit;n=number;while n>0{digit=n%10;n=n/10;sum+=digitdigitdigit;}if number==sum return true;else return false;}}50009输入一个正整数repeat 0<repeat<10,做repeat次下列运算:输入一个整数,将它逆序输出;要求定义并调用函数reversenumber,它的功能是返回number的逆序数;例如reverse12345的返回值是54321;例:括号内是说明输入4 repeat=4123456 -100 -2 99输出654321-1-299public class Test50009{public static void mainString args{int ri,repeat;long n, res;Scanner in=new Scanner;repeat=;forri=1;ri<=repeat;ri++{n=;/---------/res=reverse n;}}/---------/static long reverse long number{//返回number的逆序数int flag=1;long a=0,digit;if number<0{flag=-1;number=-number;}while number>0{digit=number%10; //分离出个位数字a=a10+digit; //形成当前的逆序数number=number/10;}return flaga;}}50011输入一个正整数repeat 0<repeat<10,做repeat次下列运算:输入三个整数a、b和c,输出其中较大的数;要求定义和调用函数maxa, b, c找出a、b中较大的数,函数形参a、b和c的类型是int;输入输出示例:括号内是说明输入3 repeat=3输入:5 8 9 a=5, b=8-1 -10 -5 a=-1, b=-101 1 1 a=1, b=1输出:max5,8,9=9max-1,-10,-5=-1max1,1,1=1public class Test50011 {public static void mainString args {int ri, repeat;int a,b,c,maximun;Scanner in = new Scanner;repeat = ;for ri = 1; ri <= repeat; ri++ {a = ;b = ;c=;/-----------------/maximun=maximuna,b,c;Sy}}/-------------------/static int maximunint a,int b,int c{int max=a;ifmax<bmax=b;ifmax<cmax=c;return max;}}。
浙大java练习题答案
40001import class Test40001 {public static void main(String[] args) { int ri, repeat;int i, n;float sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40002import class Test40002 {public static void main(String[] args) { int ri, repeat;int i, n;double fact;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40003import class Test40003 {int ri, repeat;int i, n;double x, mypow;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){x=();n=();/*--------------------*/}}}40004import class Test40004 {int ri, repeat;int i, n, flag;float sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40005import class Test40005 {public static void main(String[] args) {int ri, repeat;int i, n, temp;float sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/sum=0;for(i=1;i<=n;i++){sum=(float) (sum+(2*i-1));}}}}40006import class Test40006 {public static void main(String[] args) { int ri, repeat;int temp, flag;double eps, item, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){eps=();/*--------------------*/}}}40007import class Test40007 {public static void main(String[] args){ int ri, repeat;int begin, c, end, f;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){begin=();end=();"Celsius Fahrenheit");/*--------------------*/" "+f);}}}}40008import class Test40008 {public static void main(String[] args){ int ri, repeat;int x, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){x=();/*--------------------*/}}}}40009import class Test40009 {public static void main(String[] args){ int ri, repeat;int i , max, n, x;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}}40010import class Test40010 {public static void main(String[] args){int ri, repeat;int number, sum,n,r;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/"number="+number+", sum="+sum);}}}40011import class Test40011 {public static void main(String[] args) { int ri, repeat;int i,n;float a,b,s,t;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40012import class Test40012{public static void main(String args[]){int i, n, a, sn, tn;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){a=();n=();/*--------------------*/}}}40013import class Test40013{public static void main(String args[]){ int ri, repeat;boolean flag=true;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){m=();/*--------------------*/}if(flag) "YES");else"NO");}}}40014import .*;public class Test40014 {public static void main(String []args){Scanner in =new Scanner;int gcd, lcm, m, n,r;int repeat, ri;repeat=();for(ri = 1; ri <= repeat; ri++){m=();n=();if(m <= 0 || n <= 0)"m <= 0 or n <= 0");else{/*---------*/"the least common multiple:"+lcm+", the greatest common divisor:"+gcd);}}}}40021import class Test40021{public static void main(String args[]){ int ri, repeat;int i,n;float s,t;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40022import class Test40022{public static void main(String args[]){ int ri, repeat;int i, digit, m, n, number, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){m=();n=();/*------------------*/}}40023import class Test40023{public static void main(String args[]){int ri, repeat;int count, i, j, k, m, n, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){m=();n=();/*---------------------*/"count="+count+", sum="+sum);}}40031import class Test40031{public static void main(String []args ){ int ri, repeat,count, word,i;String line;char c;Scanner in=new Scanner;repeat=()).charAt(0)-'0'; harAt(0);/*---------*/}}}40034import class Test40034{public static void main(String []args){ int year,m,n,repeat,ri;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){m=();n=();/*---------*/}}}40035import class Test40035{public static void main(String []args){ int m,n,repeat,ri;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){n=();m=();/*---------*/}}}40036import class Test40036{public static void main(String []args){ int days,repeat,ri;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){}}}40037import class Test40037{public static void main(String []args){ int a,n,ri,count,number;double sum,ave;Scanner in=new Scanner;n=();for(ri=1;ri<=n;ri++){/。
浙大java练习题答案-40001-50004
浙大java练习题答案-40001-50004LT40001import java.util.Scanner;public class Test40001 {public static void main(String[] args) { int ri, repeat;int i, n;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(sum*1000+0.5)/10 00.);}}}40002import java.util.Scanner;public class Test40002 {public static void main(String[] args) { int ri, repeat;int i, n;double fact;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println(fact);}}}40003import java.util.Scanner;public class Test40003 {public static void main(String[] args) { int ri, repeat;int i, n;double x, mypow;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){x=in.nextDouble();n=in.nextInt();/*--------------------*/System.out.println(mypow);}}}40004import java.util.Scanner;public class Test40004 {public static void main(String[] args) {int ri, repeat;int i, n, flag;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((long)(sum*10000+0.5)/10000.);}}}40005import java.util.Scanner;public class Test40005 {public static void main(String[] args) {int ri, repeat;int i, n, temp;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/sum=0;for(i=1;i<=n;i++){sum=(float) (sum+1.0/(2*i-1));}System.out.println(sum);}}}40006import java.util.Scanner;public class Test40006 {public static void main(String[] args) {int ri, repeat;int temp, flag;double eps, item, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){eps=in.nextDouble();/*--------------------*/System.out.println((int)(sum*10000+0.5)/10000.);}}}40007import java.util.Scanner;public class Test40007 {public static void main(String[] args){ int ri, repeat;int begin, c, end, f;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){begin=in.nextInt();end=in.nextInt();System.out.println("Celsius Fahrenheit");/*--------------------*/System.out.println(c+" "+f);}}}}40008import java.util.Scanner;public class Test40008 {public static void main(String[] args){ int ri, repeat;int x, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){x=in.nextInt();/*--------------------*/}System.out.println(sum);}}}40009import java.util.Scanner;public class Test40009 {public static void main(String[] args){ int ri, repeat;int i , max, n, x;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/}System.out.println(max);}}}40010import java.util.Scanner;public class Test40010 {public static void main(String[] args){int ri, repeat;int number, sum,n,r;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println("number="+number+", sum="+sum); }}}40011import java.util.Scanner;public class Test40011 {public static void main(String[] args) {int ri, repeat;int i,n;float a,b,s,t;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(s*10000+.5)/10000.); }}}40012import java.util.Scanner;public class Test40012{public static void main(String args[]){int ri, repeat;int i, n, a, sn, tn;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){a=in.nextInt();n=in.nextInt();/*--------------------*/System.out.println(sn);}}}40013import java.util.Scanner;public class Test40013{public static void main(String args[]){ int ri, repeat;int i, m, n;boolean flag=true;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();/*--------------------*/}if(flag) System.out.println("YES");else System.out.println("NO");}}}40014import java.util.*;public class Test40014 {public static void main(String []args){Scanner in =new Scanner(System.in);int gcd, lcm, m, n,r;int repeat, ri;repeat=in.nextInt();for(ri = 1; ri <= repeat; ri++){m=in.nextInt();n=in.nextInt();if(m <= 0 || n <= 0)System.out.println("m <= 0 or n <= 0");else{/*---------*/System.out.println("the least common multiple:"+lcm+", the greatest common divisor:"+gcd);}}}}40021import java.util.Scanner;public class Test40021{public static void main(String args[]){int ri, repeat;int i,n;float s,t;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(s*10000+0.5)/10000.); }}}40022import java.util.Scanner;public class Test40022{public static void main(String args[]){ int ri, repeat;int i, digit, m, n, number, sum;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();n=in.nextInt();/*------------------*/}}}40023import java.util.Scanner;public class Test40023{public static void main(String args[]){ int ri, repeat;int count, i, j, k, m, n, sum;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();n=in.nextInt();/*---------------------*/System.out.println("count="+count+", sum="+sum); }}}40031import java.util.Scanner;public class Test40031{public static void main(String []args ){int ri, repeat,count, word,i;String line;char c;Scanner in=new Scanner(System.in);repeat=(in.nextLine()).charAt(0)-'0'; //输入repeat for(ri=1; ri<=repeat; ri++){line=in.nextLine(); //输入一行字符/*---------*/System.out.println(count);}}}40032import java.util.Scanner;public class Test40032{public static void main(String []args ){ int ri, repeat;int digit;long n, temp, pow;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextLong();/*---------*/System.out.println();}}}40033import java.util.Scanner;public class Test40033{public static void main(String args[]) { int ri, repeat;int op1, op2, res;char operator;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){op1=in.nextInt();operator =(in.next()).charAt(0);/*---------*/System.out.println(res);}}}40034import java.util.Scanner;public class Test40034{public static void main(String []args){ int year,m,n,repeat,ri;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){m=in.nextInt();n=in.nextInt();/*---------*/}}}40035import java.util.Scanner;public class Test40035{public static void main(String []args){ int m,n,repeat,ri;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();m=in.nextInt();/*---------*/}}}40036import java.util.Scanner;public class Test40036{public static void main(String []args){int days,repeat,ri;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){}}}40037import java.util.Scanner;public class Test40037{public static void main(String []args){int a,n,ri,count,number;double sum,ave;Scanner in=new Scanner(System.in);n=in.nextInt();for(ri=1;ri<=n;ri++){/。
浙大JAVA实验题答案answer
浙大J A V A实验题答案a n s w e rCKBOOD was revised in the early morning of December 17, 2020.实验8 Method的使用1.程序填空题,不要改变与输入输出有关的语句。
50001输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入1 个正整数n,计算 s 的前n项的和(保留 4 位小数)。
s = 1 + 1/2! +....+ 1/n!要求定义并调用函数fact(n)计算n的阶乘。
例:括号内是说明输入:2 (repeat=2)2 (n=2)10 (n=10)输出:1.51.7183import java.util.Scanner;public class Test50001 {public static void main(String[] args) {int ri,repeat;int i,n;double s;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*-----------*/s=0;for(i=1;i<=n;i++)s+=1.0/fact(i);System.out.println((long)(s*10000+0.5)/10000.);}}/*---------------*/static double fact(int n) {int i;double f=1;for(i=1;i<=n;i++)f*=i;return f;}}50002输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入2个正整数a和n, 求a+aa+aaa+aa…a(n个a)之和。
要求定义并调用函数fn(a,n),它的功能是返回aa…a(n个a)。
浙大JAVA-实验题答案08answerdoc资料
浙大J A V A-实验题答案08a n s w e r实验8 Method的使用1.程序填空题,不要改变与输入输出有关的语句。
50001 求1 + 1/2! +....+ 1/n!输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入1 个正整数n,计算 s 的前n项的和(保留 4 位小数)。
s = 1 + 1/2! +....+ 1/n!要求定义并调用函数fact(n)计算n的阶乘。
例:括号内是说明输入:2 (repeat=2)2 (n=2)10 (n=10)输出:1.51.7183import java.util.Scanner;public class Test50001 {public static void main(String[] args) {int ri,repeat;int i,n;double s;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*-----------*/s=0;for(i=1;i<=n;i++)s+=1.0/fact(i);System.out.println((long)(s*10000+0.5)/10000.);}}/*---------------*/static double fact(int n) {int i;double f=1;for(i=1;i<=n;i++)f*=i;return f;}}50002 求a+aa+aaa+aa…a输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入2个正整数a和n, 求a+aa+aaa+aa…a(n个a)之和。
要求定义并调用函数fn(a,n),它的功能是返回aa…a(n个a)。
浙江大学java上机参考答案48页word文档
一、求两个数的和与差。
程序填空,不要改变与输入输出有关的语句。
输入整数a和b,计算并输出a、b的和与差。
import java.io.*;import java.util.Scanner;public class Test20001{public static void main(String args[]){int a, b, sum, diff;Scanner in=new Scanner(System.in);a=in.nextInt();b=in.nextInt();sum=a+b;diff=a-b;System.out.println("The sum is "+sum);System.out.println("The difference is "+diff);二、求平方根。
程序填空,不要改变与输入输出有关的语句。
输入1个实数x,计算并输出其平方根。
例:输入1.21输出The square root of 1.21 is 1.1import java.io.*;import java.util.Scanner;public class Test20002{public static void main(String args[]){double x, root;Scanner in=new Scanner(System.in);x=in.nextDouble();r oot=Math.sqrt(x);System.out.println("The square root of "+x+" is "+root);三、华氏温度转换为摄氏温度。
程序填空,不要改变与输入输出有关的语句。
输入华氏温度f,计算并输出相应的摄氏温度c。
c = 5/9(f-32).例:括号内是说明:输入17.2 (华氏温度)输出The temprature is -8.222222222222223 import java.util.Scanner;public class Test20003 {public static void main(String[] args) {Scanner in=new Scanner(System.in);double f, c;f=in.nextDouble();c=5.0/9*(f-32);System.out.println("The temprature is "+c);四、计算旅途时间。
浙大JAVA 实验题答案11answer
实验11 数组作为方法的参数1.程序填空题,不要改变与输入输出有关的语句。
60030 编写排序函数(方法)输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数n,再输入n个整数,将它们从小到大排序后输出。
要求将排序编写为一个sort()方法。
例:括号内是说明输入3 (repeat=3)4 5 1 7 63 1 2 35 5 4 3 2 1输出1 5 6 71 2 31 2 3 4 5import java.util.Scanner;public class Test60030{public static void main(String []args){int ri, repeat;int i, n, a[];Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();a=new int[n];for(i=0; i<n; i++)a[i]=in.nextInt();sort(a);for(i=0; i<n; i++)System.out.print(a[i]+" ");System.out.println();}}/*---------*///说明:数组作参数时,传递的是地址,形参数组和实参数组共用同一块内存,//方法sort()中对形参数组b排序,实际上就是对实参数组a排序,//所以不需要返回值。
static void sort(int b[]){int i,j,k,temp;for(i=0;i<b.length-1;i++) { //选择法递增排序k=i;for(j=i+1;j<b.length;j++)if(b[k]>b[j]) k=j;if(i!=k){temp=b[i];b[i]=b[k]; b[k]=temp;}}}}60031 判断两个矩阵是否相同(方法)输入一个正整数repeat (0<repeat<10),做repeat次下列运算:读入 1 个正整数n(1≤n≤6), 再读入2个 n 阶方阵 a和b , 判断2个方阵是否相同。
浙大java练习题答案-40001-50004
40001import java.util.Scanner;public class Test40001 {public static void main(String[] args) {int ri, repeat;int i, n;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(sum*1000+0.5)/1000.);}}}40002import java.util.Scanner;public class Test40002 {public static void main(String[] args) { int ri, repeat;int i, n;double fact;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println(fact);}}}40003import java.util.Scanner;public class Test40003 {public static void main(String[] args) { int ri, repeat;int i, n;double x, mypow;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){x=in.nextDouble();n=in.nextInt();/*--------------------*/System.out.println(mypow);}}}40004import java.util.Scanner;public class Test40004 {public static void main(String[] args) {int ri, repeat;int i, n, flag;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((long)(sum*10000+0.5)/10000.);}}}40005import java.util.Scanner;public class Test40005 {public static void main(String[] args) {int ri, repeat;int i, n, temp;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/sum=0;for(i=1;i<=n;i++){sum=(float) (sum+1.0/(2*i-1));}System.out.println(sum);}}}40006import java.util.Scanner;public class Test40006 {public static void main(String[] args) { int ri, repeat;int temp, flag;double eps, item, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){eps=in.nextDouble();/*--------------------*/System.out.println((int)(sum*10000+0.5)/10000.);}}}40007import java.util.Scanner;public class Test40007 {public static void main(String[] args){int ri, repeat;int begin, c, end, f;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){begin=in.nextInt();end=in.nextInt();System.out.println("Celsius Fahrenheit");/*--------------------*/System.out.println(c+" "+f);}}}}40008import java.util.Scanner;public class Test40008 {public static void main(String[] args){ int ri, repeat;int x, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){x=in.nextInt();/*--------------------*/}System.out.println(sum);}}}40009import java.util.Scanner;public class Test40009 {public static void main(String[] args){ int ri, repeat;int i , max, n, x;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/}System.out.println(max);}}}40010import java.util.Scanner;public class Test40010 {public static void main(String[] args){ int ri, repeat;int number, sum,n,r;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println("number="+number+", sum="+sum); }}}40011import java.util.Scanner;public class Test40011 {public static void main(String[] args) {int ri, repeat;int i,n;float a,b,s,t;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(s*10000+.5)/10000.); }}}40012import java.util.Scanner;public class Test40012{public static void main(String args[]){int ri, repeat;int i, n, a, sn, tn;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){a=in.nextInt();n=in.nextInt();/*--------------------*/System.out.println(sn);}}}40013import java.util.Scanner;public class Test40013{public static void main(String args[]){ int ri, repeat;int i, m, n;boolean flag=true;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();/*--------------------*/}if(flag) System.out.println("YES");else System.out.println("NO");}}}40014import java.util.*;public class Test40014 {public static void main(String []args){ Scanner in =new Scanner(System.in);int gcd, lcm, m, n,r;int repeat, ri;repeat=in.nextInt();for(ri = 1; ri <= repeat; ri++){m=in.nextInt();n=in.nextInt();if(m <= 0 || n <= 0)System.out.println("m <= 0 or n <= 0");else{/*---------*/System.out.println("the least common multiple:"+lcm+", the greatest common divisor:"+gcd);}}}}40021import java.util.Scanner;public class Test40021{public static void main(String args[]){int ri, repeat;int i,n;float s,t;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(s*10000+0.5)/10000.); }}}40022import java.util.Scanner;public class Test40022{public static void main(String args[]){int ri, repeat;int i, digit, m, n, number, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();n=in.nextInt();/*------------------*/}}}40023import java.util.Scanner;public class Test40023{public static void main(String args[]){int ri, repeat;int count, i, j, k, m, n, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();n=in.nextInt();/*---------------------*/System.out.println("count="+count+", sum="+sum); }}}40031import java.util.Scanner;public class Test40031{public static void main(String []args ){int ri, repeat,count, word,i;String line;char c;Scanner in=new Scanner(System.in);repeat=(in.nextLine()).charAt(0)-'0'; //输入repeat for(ri=1; ri<=repeat; ri++){line=in.nextLine(); //输入一行字符/*---------*/System.out.println(count);}}}40032import java.util.Scanner;public class Test40032{public static void main(String []args ){int ri, repeat;int digit;long n, temp, pow;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextLong();/*---------*/System.out.println();}}}40033import java.util.Scanner;public class Test40033{public static void main(String args[]) { int ri, repeat;int op1, op2, res;char operator;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){op1=in.nextInt();operator =(in.next()).charAt(0);/*---------*/System.out.println(res);}}}40034import java.util.Scanner;public class Test40034{public static void main(String []args){ int year,m,n,repeat,ri;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){m=in.nextInt();n=in.nextInt();/*---------*/}}}40035import java.util.Scanner;public class Test40035{public static void main(String []args){ int m,n,repeat,ri;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();m=in.nextInt();/*---------*/}}}40036import java.util.Scanner;public class Test40036{public static void main(String []args){ int days,repeat,ri;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){}}}40037import java.util.Scanner;public class Test40037{public static void main(String []args){int a,n,ri,count,number;double sum,ave;Scanner in=new Scanner(System.in);n=in.nextInt();for(ri=1;ri<=n;ri++){/。
浙大《Java语言程序设计》编程答案4
实验5分支结构程序的设计程序填空,不要改变与输入输出有关的语句。
一、显示两级成绩输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个学生的数学成绩,如果它低于60,输出“Fail”,否则,输出“Pass”。
输出使用"Pass");及"Fail");例:括号内是说明输入2 (repeat=2)60 59输出PassFailimport ;public class Test30001{public static void main(String[] args){int ri, repeat;int mark;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){mark=in.nextInt();/*------------------*/if (mark<60)"Fail");else"Pass");}}}二、找最小值输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入四个整数,输出其中的最小值。
例:括号内是说明输入3 (repeat=3)12 6 1 9010 40 30 20-1 -3 -4 -5输出min is 1 (12 6 1 90 中的最小值是1)min is 10 (10 40 30 20 中的最小值是10)min is -5 (-1 -3 -4 -5中的最小值是-5)import ;public class Test30002 {public static void main(String[] args){int ri, repeat;int a, b, c, d, min;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){a=in.nextInt();b=in.nextInt();c=in.nextInt();d=in.nextInt();/*------------------*/min=a;if(min>b) min=b;if(min>c) min=c;if(min>d) min=d;"min is "+min);}}}三、求三角形的面积和周长输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入三角形的三条边a, b, c,如果能构成一个三角形,输出面积area和周长perimeter;否则,输出“These sides do not correspond to a valid triangle”。
浙大 Java语言程序设计编程答案5
实验6 循环结构程序的设计(一)程序填空,不要改变与输入输出有关的语句。
1. 求1+1/2+1/3+……+1/n输入一个正整数repeat (0<repeat<10),做repeat次下列运算:读入1 个正整数 n(n<=100),计算并输出1+1/2+1/3+……+1/n 。
例:括号内是说明输入2 (repeat=1)210输出1.52.9289684import java.util.Scanner;public class Test40001 {public static void main(String[] args) {int ri, repeat;int i, n;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/sum=0;//注意这条赋初值语句的位置for(i=1;i<=n;i++){sum+=1.0f/i;}System.out.println((int)(sum*1000+0.5)/1000.);}}}2. 求n!输入一个正整数repeat (0<repeat<10),做repeat次下列运算:读入1 个正整数 n(n<=50),计算并输出n! 。
例:括号内是说明输入3 (repeat=2)1105输出1.03628800.0120.0import java.util.Scanner;public class Test40002 {public static void main(String[] args) {int ri, repeat;int i, n;double fact;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/fact=1;for(i=1;i<=n;i++)fact*=i;System.out.println(fact);}}}3. 求x的n次幂输入一个正整数repeat (0<repeat<10),做repeat次下列运算:读入1 个正实数x和1个正整数 n(n<=50),计算并输出x的n次幂。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验9-1 Method的使用(二)1.程序填空题,不要改变与输入输出有关的语句。
50010 十进制转换二进制输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:输入1 个正整数n,将其转换为二进制后输出。
要求定义并调用函数 dectobin(n),它的功能是输出 n 的二进制。
例如,调用dectobin(10),输出1010。
输出语句:..+ 1/n!输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入1 个正整数n,计算 s 的前n项的和(保留 4 位小数)。
s = 1 + 1/2! +....+ 1/n!要求定义并调用函数fact(n)计算n的阶乘。
例:括号内是说明输入:2 (repeat=2)2 (n=2)10 (n=10)输出:import class Test50001 {public static void main(String[] args) {int ri,repeat;int i,n;double s;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){n=();/*-----------*/s=0;for(i=1;i<=n;i++)s+=fact(i);}}/*---------------*/static double fact(int n) {....要求定义并调用函数fib(n),它的功能是返回第n项Fibonacci数。
例如,fib(7)的返回值是13。
输出语句:" ");例:括号内是说明输入:3 (repeat=3)1 10 (m=1, n=10)20 100 (m=20, n=100)1000 6000 (m=1000, n=6000)输出:1 123 5 8 (1到10之间的Fibonacci数)21 34 55 89 (20到100之间的Fibonacci数)1597 2584 4181 (1000到6000之间的Fibonacci数)import class Test50006{public static void main(String args[]){int ri,repeat;int i, m, n;long f;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){m=();n=();/*---------*/i=1;f=1;while(f<=n){if(f>=m) " ");i++;f=fib(i);}}}/*------------*/static long fib(int n){ //递归方法if(n==1||n==2) return 1;else return fib(n-1)+fib(n-2);}}实验9-2 一维数组的使用1.求平均值输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数n (1<n≤10),再输入n个整数,输出平均值。
例:括号内是说明输入2 (repeat=2)3 1 2 -65 12 2 5 4 0输出aver=aver=import class Test60001{public static void main(String []args){int ri, repeat;int i, n, sum,a[];float aver;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();a=new int[n];for(i=0; i<n; i++)a[i]=();//这个循环输入数组各元素/*--------------*/sum=0;for(i=0; i<n; i++) //这个循环实现累加sum+=a[i];aver=(float)sum/n; //求平均值,注意要先把sum转换成float,再计算"aver="+aver);}}}2.求最大值及其下标输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数n (1<n≤10),再输入n个整数,输出最大值极其下标(设最大值惟一,下标从0开始)。
例:括号内是说明输入3 (repeat=3)3 1 6 43 10 8 15 1 2 5 4 0输出max=6,index=1 (最大值6的下标是1)max=10,index=0 (最大值10的下标是0)max=5,index=2 (最大值5的下标是2)import class Test60002{public static void main(String []args){int ri, repeat;int i, index, n, a[];Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();a=new int[n] ;for(i=0; i<n; i++)a[i]=();/*--------------*/index=0; //用index保存最大数的下标,开始假设a[0]是最大数for(i=1; i<n; i++)if(a[index]<a[i]) index=i;//a[i]与当前最大数a[index]比较,若a[i]更大,index变为i"max="+a[index]+",index="+index);}}}3.逆序输出输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数n (1<n≤10),再输入n个整数,按逆序输出这些数。
例:括号内是说明输入2 (repeat=2)4 10 8 1 25 1 2 5 4 0输出2 1 8 100 4 5 2 1import class Test60003{public static void main(String []args){int ri, repeat;int i, n, temp,a[];Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();a=new int[n];for(i=0; i<n; i++)a[i]=();/*--------------*/for(i=0; i<n/2; i++){ //a[i]与a[n-1-i]交换,注意交换次数temp=a[i];a[i]=a[n-1-i];a[n-1-i]=temp;}for(i=0; i<n; i++)//输出一个数组" ");}}}4.交换最小值和最大值输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数n,再输入n个整数,将最小值与第一个数交换,最大值与最后一个数交换,然后输出交换后的n个数。
例:括号内是说明输入3 (repeat=3)5 4 3 5 1 24 156 75 5 4 3 2 1输出1 32 4 51 5 6 71 4 32 5import class Test60004{public static void main(String []args){int ri, repeat;int i, index, n, t,a[];Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();a=new int[n];for(i=0; i<n; i++)a[i]=();/*--------------*/index=0; //找最小数,与第一个数交换for(i=0; i<n; i++)if(a[index]>a[i]) index=i;t=a[index]; a[index]=a[0];a[0]=t; //交换index=0; //找最大数,与最后一个数交换for(i=0; i<n; i++)if(a[index]<a[i]) index=i;t=a[index]; a[index]=a[n-1];a[n-1]=t; //交换for(i=0; i<n; i++)" ");}}}说明:不能把两个循环合成一个,否则当数组元素为“5 4 3 2 1”时出错。