Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Gauss {
  3.     public static class G {
  4.         int ch;
  5.         int zn;
  6.         G(int ch1, int zn1) {
  7.             if (zn1 < 0) {
  8.                 ch1 = -ch1;
  9.                 zn1 = -zn1;
  10.             }
  11.             ch = ch1 / nod(ch1, zn1);
  12.             zn = zn1 / nod(ch1,zn1);
  13.         }
  14.         public G sbst(G pr) {
  15.             int red = znm(zn, pr.zn);
  16.             return new G(ch * red / zn - pr.ch * red / pr.zn, red);
  17.         }
  18.         public G mult(G pr) {
  19.             return new G(ch * pr.ch, zn * pr.zn);
  20.         }
  21.         public G del(G pr) {
  22.             return new G(ch * pr.zn, zn * pr.ch);
  23.         }
  24.         public String toString() {
  25.             return ch + "/" + zn;
  26.         }
  27.         int nod(int x, int y) {
  28.             return y != 0 ? nod(y, x % y) : Math.abs(x);
  29.         }
  30.         int znm(int x, int y) {
  31.             return x * (y / nod(x, y));
  32.         }
  33.         int chi(int x, int y) {
  34.             return y * (x / nod(x, y));
  35.         }
  36.  
  37.     }
  38.     public static void main(String[] args) {
  39.         Scanner in = new Scanner(System.in);
  40.         int n = in.nextInt(), i,j;
  41.         G x[] = new G[n];
  42.         G matr;
  43.         G slay[][] = new G[n][n+1];
  44.         for(i = 0; i < n; i++)
  45.             for(j = 0; j < n+1; j++)
  46.                 slay[i][j] = new G(in.nextInt(),1);
  47.         for(i = 0; i < n; i++) {
  48.             for (j = i; j < n; j++) {
  49.                 if (slay[j][i].ch != 0) {
  50.                     G[] vs = slay[i];
  51.                     slay[i] = slay[j];
  52.                     slay[j] = vs;
  53.                     for (int z = i + 1; z < n; z++) {
  54.                         matr = slay[z][i].del(slay[i][i]);
  55.                         for (int z1 = i; z1 < n + 1; z1++)
  56.                             slay[z][z1] = slay[z][z1].sbst(matr.mult(slay[i][z1]));
  57.                     }
  58.                     break;
  59.                 }
  60.                 if (j == n - 1) {
  61.                     System.out.println("No solution");
  62.                     System.exit(0);
  63.                 }
  64.             }
  65.         }
  66.         for(i = n - 1; i >= 0; i--) {
  67.             for (j = n - 1; j > i; j--)
  68.                 slay[i][n] = slay[i][n].sbst(slay[i][j].mult(x[j]));
  69.             x[i] = slay[i][n].del(slay[i][j]);
  70.         }
  71.         for(i = 0; i < x.length; i++)
  72.             System.out.println(x[i]);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement