HDU 1840 Equations (简单数学 + 水题)(Java版)

时间:2021-10-15 19:47:32

Equations

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1840

    ——每天在线,欢迎留言谈论。

题目大意:

  给你一个一元二次方程组,a(X^2) + b(X) + c = 0 。求X解的个数。

思路:

  分别讨论二次方程与一次方程的情况,再特殊处理下 a = b = c = 0 的情况。

感想:

  是时候该水水题了。

Java AC代码:

 import java.math.*;
import java.util.Scanner; public class Main {
static Scanner scn = new Scanner(System.in); public static void main(String[] args) {
int t, a, b, c, answer;
t = scn.nextInt();
while (t-- > 0) {
a = scn.nextInt();
b = scn.nextInt();
c = scn.nextInt();
answer = Tool.getAns(a, b, c);
if (answer == -1)
System.out.println("INF");
else
System.out.println(answer);
}
System.exit(0);
}
} class Tool {
public static int getAns(int a, int b, int c) {
if (a == 0) {
if (b == 0) {
if (c == 0)
return -1;
else
return 0;
}
return 1;
} else {
int o = (int)Math.pow(b, 2) - 4 * a * c;
if (o < 0)
return 0;
else if (o == 0)
return 1;
else
return 2;
}
}
}

2017-08-10 19:16:00