Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Gauss {
- public static class G {
- int ch;
- int zn;
- G(int ch1, int zn1) {
- if (zn1 < 0) {
- ch1 = -ch1;
- zn1 = -zn1;
- }
- ch = ch1 / nod(ch1, zn1);
- zn = zn1 / nod(ch1,zn1);
- }
- public G sbst(G pr) {
- int red = znm(zn, pr.zn);
- return new G(ch * red / zn - pr.ch * red / pr.zn, red);
- }
- public G mult(G pr) {
- return new G(ch * pr.ch, zn * pr.zn);
- }
- public G del(G pr) {
- return new G(ch * pr.zn, zn * pr.ch);
- }
- public String toString() {
- return ch + "/" + zn;
- }
- int nod(int x, int y) {
- return y != 0 ? nod(y, x % y) : Math.abs(x);
- }
- int znm(int x, int y) {
- return x * (y / nod(x, y));
- }
- int chi(int x, int y) {
- return y * (x / nod(x, y));
- }
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt(), i,j;
- G x[] = new G[n];
- G matr;
- G slay[][] = new G[n][n+1];
- for(i = 0; i < n; i++)
- for(j = 0; j < n+1; j++)
- slay[i][j] = new G(in.nextInt(),1);
- for(i = 0; i < n; i++) {
- for (j = i; j < n; j++) {
- if (slay[j][i].ch != 0) {
- G[] vs = slay[i];
- slay[i] = slay[j];
- slay[j] = vs;
- for (int z = i + 1; z < n; z++) {
- matr = slay[z][i].del(slay[i][i]);
- for (int z1 = i; z1 < n + 1; z1++)
- slay[z][z1] = slay[z][z1].sbst(matr.mult(slay[i][z1]));
- }
- break;
- }
- if (j == n - 1) {
- System.out.println("No solution");
- System.exit(0);
- }
- }
- }
- for(i = n - 1; i >= 0; i--) {
- for (j = n - 1; j > i; j--)
- slay[i][n] = slay[i][n].sbst(slay[i][j].mult(x[j]));
- x[i] = slay[i][n].del(slay[i][j]);
- }
- for(i = 0; i < x.length; i++)
- System.out.println(x[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement