a006. 一元二次方程式 java

題目:http://zerojudge.tw/ShowProblem?problemid=a006
程式碼:

package a006;
import java.util.Scanner;
public class a006 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc = new Scanner(System.in);
  while(sc.hasNext()){
   int a = sc.nextInt();
   int b = sc.nextInt();
   int c = sc.nextInt();
   int d = b*b-4*a*c;
   if(d>0){
    System.out.println("Two different roots x1="+(int)(-1*b+(Math.sqrt(d)))/2+" , x2="+(int)(-1*b-(Math.sqrt(d)))/2);
   }else if(d==0){
    System.out.println("Two same roots x="+-1*(b/(2*a)));
   }else {
    System.out.println("No real root");
   }
  }

 }

}

留言

熱門文章