Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Rational {
  4. int x, y;
  5.  
  6. static Rational[][] a;
  7.  
  8. public static Rational CAN(Rational x) {
  9. int a = x.x;
  10. int b = x.y;
  11. Rational d= new Rational();
  12. if (a == 0) {
  13. d.y = 1;
  14. return d;
  15. }
  16. if (a < 0) {
  17. a = -a;
  18. }
  19. if (b < 0) {
  20. d.x = -d.x;
  21. d.y = -d.y;
  22. b = -b;
  23. }
  24. if (b < a) {
  25. int c;
  26. c = a;
  27. a = b;
  28. b = c;
  29. }
  30. while (a!=0 && (b % a) != 0) {
  31. b = a;
  32. a = b % a;
  33. }
  34. if(a!=0) {
  35. d.x /= a;
  36. d.y /= a;
  37. }
  38. return d;
  39. }
  40.  
  41. public static Rational ADD(Rational x, Rational y) {
  42. Rational res;
  43. res=new Rational();
  44. res.x = x.x * y.y + y.x * x.y;
  45. res.y = y.y * x.y;
  46. return CAN(res);
  47. }
  48.  
  49. public static Rational SUB(Rational x, Rational y) {
  50. Rational d= new Rational();
  51. d.x = -y.x;
  52. d.y=y.y;
  53. return ADD(x, d);
  54. }
  55.  
  56. public static Rational MUL(Rational x, Rational y) {
  57. Rational res;
  58. res = new Rational();
  59. res.x = x.x * y.x;
  60. res.y = x.y * y.y;
  61. return CAN(res);
  62. }
  63.  
  64. public static Rational DIV(Rational x, Rational y) {
  65. Rational d=new Rational();
  66. d.x = y.y;
  67. d.y = y.x;
  68. return MUL(x, d);
  69. }
  70.  
  71. public static void swap_str(int x, int y, int n) {
  72. Rational c;
  73. int i;
  74. for (i = 0; i < n; i++) {
  75. c = a[x][i];
  76. a[x][i] = a[y][i];
  77. a[y][i] = c;
  78. }
  79. }
  80.  
  81. public static void main(String[] args) {
  82. int i, j, fs = 1, k, sc;
  83. Scanner in = new Scanner(System.in);
  84. int n = in.nextInt();
  85. Rational[] x = new Rational[n];
  86. Rational b;
  87. a = new Rational[n][n+1];
  88. for (i = 0; i < n; i++) {
  89. for (j = 0; j <= n; j++) {
  90. a[i][j] = new Rational();
  91. sc = in.nextInt();
  92. a[i][j].x = sc;
  93. a[i][j].y = 1;
  94. }
  95. }
  96. for (k = 0; k < n; k++) {
  97. if (k > 0) {
  98. for (j = k; j < n; j++) {
  99. Rational[] t = a[k];
  100. a[k]=a[j];
  101. a[j]=t;
  102. b = DIV(a[j][k - 1], a[k - 1][k - 1]);
  103. for (i = 0; i <= n; i++) {
  104. a[j][i] = SUB(a[j][i], MUL(b, a[k - 1][i]));
  105. }
  106. }
  107. }
  108. if (a[k][k].x == 0) {
  109. if (k + fs > n - 1) {
  110. System.out.printf("No solution\n");
  111. return;
  112. }
  113. swap_str(k, k + fs, n + 1);
  114. k--;
  115. fs++;
  116. } else {
  117. fs = 1;
  118. }
  119. }
  120. for (i = n - 1; i >= 0; i--) {
  121. x[i] = a[i][n];
  122. for (j = n - 1; j > i; j--) {
  123. x[i] = SUB(x[i], MUL(a[i][j], x[j]));
  124. }
  125. x[i] = DIV(x[i], a[i][i]);
  126. }
  127. for (i = 0; i < n; i++){
  128. System.out.println(x[i].x+"/"+x[i].y);
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement