Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math"
- "math/big"
- )
- func main() {
- var n int
- var temp int64
- fmt.Scan(&n)
- B := make([]big.Rat, n)
- A := make([][]big.Rat, n)
- for i := 0; i < n; i++ {
- A[i] = make([]big.Rat, n)
- for j := 0; j < n; j++ {
- fmt.Scan(&temp)
- A[i][j].SetInt64(temp)
- }
- fmt.Scan(&temp)
- B[i].SetInt64(temp)
- }
- k := 0
- max := big.NewRat(math.MinInt32,1)
- eps := big.NewRat(1,100000)
- var tmp *big.Rat
- for k < n{
- max = big.NewRat(A[k][k].Num().Int64(),A[k][k].Denom().Int64())
- max.Abs(max)
- index := k
- for i := k+1; i < n; i++{
- tmp = big.NewRat(A[i][k].Num().Int64(),A[i][k].Denom().Int64())
- tmp.Abs(tmp)
- if tmp.Cmp(max) == 1 {
- max = tmp
- index = i
- }
- }
- if eps.Cmp(max) == 1{
- fmt.Println("No solution")
- return
- }
- for i := 0; i < n; i++{
- A[index][i],A[k][i] = A[k][i], A[index][i]
- }
- B[index],B[k] = B[k], B[index]
- for i := k; i < n; i++{
- tmp = big.NewRat(A[i][k].Num().Int64(),A[i][k].Denom().Int64())
- if eps.Cmp(tmp.Abs(tmp)) == 1{continue}
- tmp = big.NewRat(A[i][k].Num().Int64(),A[i][k].Denom().Int64())
- if tmp.Cmp(big.NewRat(0,1)) != 0{tmp.Inv(tmp)}
- for j := 0; j < n; j++{
- A[i][j].Mul(&A[i][j],tmp)
- }
- B[i].Mul(&B[i],tmp)
- if i == k{continue}
- for j := 0; j < n; j++ {A[i][j].Sub(&A[i][j],&A[k][j])}
- B[i].Sub(&B[i],&B[k])
- }
- k++
- }
- var buffer []string
- for i := n-1; i >= 0; i--{
- buffer = append(buffer, B[i].String())
- for j := 0; j < i; j++{
- tmp = big.NewRat(A[j][i].Num().Int64(),A[j][i].Denom().Int64())
- tmp.Mul(tmp,&B[i])
- B[j].Sub(&B[j],tmp)
- }
- }
- for len(buffer) != 0{
- l := len(buffer)
- fmt.Println(buffer[l-1])
- buffer = buffer[:l-1]
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement