欢迎来到天天文库
浏览记录
ID:14673543
大小:156.00 KB
页数:14页
时间:2018-07-29
《浙大 java语言程序设计编程答案》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验13未做和选做题1.求一元二次方程的根 输入一个正整数repeat(02、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.Scan3、ner;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,4、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(x5、1*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"6、);}}}}}说明:为防止出错,在编程前应考虑周全²当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)若d<0,有两个不同复根: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.printl7、n("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);}e8、lse{x1=-(double)b/(2*a);x2=Math.sqrt(-d)
2、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.Scan
3、ner;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,
4、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(x
5、1*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"
6、);}}}}}说明:为防止出错,在编程前应考虑周全²当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)若d<0,有两个不同复根: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.printl
7、n("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);}e
8、lse{x1=-(double)b/(2*a);x2=Math.sqrt(-d)
此文档下载收益归作者所有