您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 浙大-Java语言程序设计编程答案12
实验13未做和选做题1.求一元二次方程的根输入一个正整数repeat(0repeat10),做repeat次下列运算:输入参数a,b,c,求一元二次方程a*x*x+b*x+c=0的根,结果保留2位小数(如果答案为3.50,则直接显示为3.5)。按情况分别输出以下内容(文字中的标点为西文标点,无空格。括号内为说明):(1)a=b=c=0,meaningless(2)a=b=0,c!=0,error(3)x=值(值为-c/b)(4)x1=值1(值1为(-b+Math.sqrt(d))/(2*a))x2=值2(值2为(-b-Math.sqrt(d))/(2*a))(5)x1=值1+值2ix2=值1-值2i(值1为-b/(2*a),值2为Math.sqrt(-d)/(2*a))输入输出示例:括号内为说明输入:5(repeat=5)000(a=0,b=0,c=0)001(a=0,b=0,c=1)025(a=0,b=2,c=5)218935(a=21,b=89,c=35)223(a=2,b=2,c=3)输出:a=b=c=0,meaninglessa=b=0,c!=0,errorx=-2.5x1=-0.44x2=-3.8x1=-0.5+1.12ix2=-0.5-1.12iimportjava.util.Scanner;publicclassTest30009{publicstaticvoidmain(Stringargs[]){intrepeat,ri;inta,b,c,d;doublex1,x2;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri=repeat;ri++){a=in.nextInt();b=in.nextInt();c=in.nextInt();/*---------*/if(a==0&&b==0&&c==0)System.out.println(a=b=c=0,meaningless);elseif(a==0&&b==0&&c!=0)System.out.println(a=b=0,c!=0,error);elseif(a==0&&b!=0){x1=-(double)c/b;System.out.println(x=+Math.round(x1*100)/100.0);}else{d=b*b-4*a*c;if(d=0){x1=(-b+Math.sqrt(d))/(2*a);x2=(-b-Math.sqrt(d))/(2*a);System.out.println(x1=+Math.round(x1*100)/100.0);System.out.println(x2=+Math.round(x2*100)/100.0);}else{x1=-(double)b/(2*a);x2=Math.sqrt(-d)/(2*a);System.out.println(x1=+Math.round(x1*100)/100.0+++Math.round(x2*100)/100.0+i);System.out.println(x2=+Math.round(x1*100)/100.0+-+Math.round(x2*100)/100.0+i);}}}}}说明:为防止出错,在编程前应考虑周全当a=b=c=0时,方程有无数解;当a=b=0,c!=0时,方程无解;当a=0,b!=0时,方程解为x=-c/b;当a!=0时,求:d=b*b-4*a*c若d=0,有两个不同实根:x=(-b±Math.sqrt(d))/(2*a)若d0,有两个不同复根:x=-b/(2*a)±Math.sqrt(-d)/(2*a)i或:if(a==0){if(b==0){if(c==0)System.out.println(a=b=c=0,meaningless);elseSystem.out.println(a=b=0,c!=0,error);}else{x1=-(double)c/b;System.out.println(x=+Math.round(x1*100)/100.0);}}else{d=b*b-4*a*c;if(d=0){x1=(-b+Math.sqrt(d))/(2*a);x2=(-b-Math.sqrt(d))/(2*a);System.out.println(x1=+Math.round(x1*100)/100.0);System.out.println(x2=+Math.round(x2*100)/100.0);}else{x1=-(double)b/(2*a);x2=Math.sqrt(-d)/(2*a);System.out.println(x1=+Math.round(x1*100)/100.0+++Math.round(x2*100)/100.0+i);System.out.println(x2=+Math.round(x1*100)/100.0+-+Math.round(x2*100)/100.0+i);}}2.输出m~n之间的所有闰年(未做)输入一个repeat(1repeat10)),做repeat次下列运算:输入正整数m和n(1900=m=n=2100),分别代表年份的起止值,输出该范围内所有的闰年。判断闰年的条件是:能被4整除但不能被100整除,或者能被400整除。输出使用语句System.out.println(year);输入输出示例:括号内是说明输入:22000201720302040输出20002004200820122016203220362040importjava.util.Scanner;publicclassTest40034{publicstaticvoidmain(String[]args){intyear,m,n,repeat,ri;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri=repeat;ri++){m=in.nextInt();n=in.nextInt();/*---------*/for(year=m;year=n;year++){if((year%4==0&&year%100!=0)||year%400==0)System.out.println(year);}}}3.简单计算器(选做)40033输入一个正整数repeat(0repeat10),做repeat次下列运算:程序模拟简单运算器的工作:输入一个算式,遇等号=说明输入结束,输出结果。假设计算器只能进行加减乘除运算,运算数和结果都是整数,4种运算符的优先级相同,按从左到右的顺序计算。例:括号内是说明输入2(repeat=2)15+2/3=(数字与运算符之间有一空格)1+2*10-10/2=(数字与运算符之间有一空格)输出510importjava.util.Scanner;publicclassT40033{publicstaticvoidmain(Stringargs[]){intri,repeat;intop1,op2,res;charoperator;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri=repeat;ri++){op1=in.nextInt();operator=(in.next()).charAt(0);/*---------*/res=op1;while(operator!='='){op2=in.nextInt();switch(operator){case'+':res+=op2;break;case'-':res-=op2;break;case'*':res*=op2;break;case'/':res/=op2;break;}operator=(in.next()).charAt(0);}System.out.println(res);}}}4.找鞍点(选做)输入一个正整数repeat(0repeat10),做repeat次下列运算:输入1个正整数n和n阶方阵a中的元素,如果找到a的鞍点(鞍点的元素值在该行上最大,在该列上最小),就输出它的下标,否则,输出NO(设a最多有1个鞍点)。例:括号内是说明输入:2(repeat=2)41741483616120789(n=4)21741(n=2)输出:a[2][1]=6NOimportjava.util.Scanner;publicclassTest60019{publicstaticvoidmain(String[]args){intri,repeat;inti,j,k,row,col,n,a[][];booleanflag;Scannerin=newScanner(System.in);repeat=in.nextInt();for(ri=1;ri=repeat;ri++){n=in.nextInt();a=newint[n][n];for(i=0;in;i++)for(j=0;jn;j++)a[i][j]=in.nextInt();/*---------*/flag=true;row=0;col=0;//不写这些,就通不过编译for(i=0;in;i++){//先在第i行中找出该行的最大值row=i;//row保存最大值的行坐标col=0;//col保存最大值的列坐标,假设第i行第0列元素为最大for(j=1;jn;j++)if(a[row][j]a[row][col])col=j;//再判断a[row][col]是否是第col列中的最小值flag=true;for(k=0;kn;k++)if(a[k][col]a[row][col]){flag=false;break;//若有比a[row][col]小的,可知它不是鞍点,可结束循环}if(flag)break;//找到1个鞍点后就不用再找了}if(flag)System.out.println(a[+row+][+col+]=+a[row][col]);elseSystem.out.println(NO);}}}
本文标题:浙大-Java语言程序设计编程答案12
链接地址:https://www.777doc.com/doc-4514517 .html