【HDU】2449 Gauss Elimination

题意:给出n个未知数的方程组,求未知数。不是正数的用分数表示。

赤裸裸的Java暴力,消元的时候用乘法相消,避免除法。

 1 import java.util.*;

 2 import java.math.*;

 3 

 4 public class Main {

 5     public static BigInteger g[][] = new BigInteger[110][110];

 6 

 7     public static boolean Gauss(int n) {

 8         BigInteger tmp, a, b;

 9         int i, j, k;

10         for (i = 0; i < n; i++) {

11             for (j = i; j < n; j++) {

12                 if (g[j][i].compareTo(BigInteger.ZERO) != 0)

13                     break;

14             }

15             if (j >= n)

16                 return false;

17             if (j != i) {

18                 for (k = 0; k <= n; k++) {

19                     tmp = g[i][k];

20                     g[i][k] = g[j][k];

21                     g[j][k] = tmp;

22                 }

23             }

24             a = g[i][i];

25             for (j = i + 1; j < n; j++) {

26                 if (g[j][i].compareTo(BigInteger.ZERO) != 0) {

27                     b = g[j][i];

28                     for (k = i; k <= n; k++) {

29                         g[j][k] = g[j][k].multiply(a).subtract(

30                                 g[i][k].multiply(b));

31                     }

32                 }

33             }

34         }

35         return true;

36     }

37 

38     public static void main(String[] args) {

39         Scanner in = new Scanner(System.in);

40         BigInteger x[] = new BigInteger[110];

41         BigInteger y[] = new BigInteger[110];

42         BigInteger tmp, up, down;

43         int n, i, j;

44         boolean neg;

45         while (in.hasNext()) {

46             n = in.nextInt();

47             for (i = 0; i < n; i++) {

48                 for (j = 0; j <= n; j++)

49                     g[i][j] = in.nextBigInteger();

50             }

51             if (Gauss(n)) {

52                 for (i = n - 1; i >= 0; i--) {

53                     up = BigInteger.ZERO;

54                     down = BigInteger.ONE;

55                     for (j = i + 1; j < n; j++) {

56                         up = y[j].multiply(up).add(

57                                 g[i][j].multiply(x[j].multiply(down)));

58                         down = y[j].multiply(down);

59                     }

60                     up = g[i][n].multiply(down).subtract(up);

61                     down = g[i][i].multiply(down);

62                     if (up.multiply(down).compareTo(BigInteger.ZERO) < 0)

63                         neg = true;

64                     else

65                         neg = false;

66                     up = up.abs();

67                     down = down.abs();

68                     tmp = up.gcd(down);

69                     x[i] = up.divide(tmp);

70                     y[i] = down.divide(tmp);

71                     if (neg)

72                         x[i] = x[i].negate();

73                 }

74                 for (i = 0; i < n; i++) {

75                     if (x[i].mod(y[i]).compareTo(BigInteger.ZERO) == 0)

76                         System.out.println(x[i].divide(y[i]));

77                     else

78                         System.out.println(x[i] + "/" + y[i]);

79                 }

80             } else

81                 System.out.println("No solution.");

82             System.out.println();

83         }

84     }

85 }

你可能感兴趣的:(Mina)