Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math"
- )
- type Ratio struct {
- num int
- denum int
- }
- type stack []string
- func (s *stack) Push(v string){
- *s = append(*s, v)
- }
- func (s *stack) Pop() string {
- if len(*s) == 0{
- panic(s)
- }
- l := len(*s)
- val := (*s)[l-1]
- *s = (*s)[:l-1]
- return val
- }
- func (s *stack) IsEmpty() bool{
- return len(*s) == 0
- }
- func ScanRatio(ratio *Ratio){
- fmt.Scan(&ratio.num)
- ratio.denum = 1
- }
- func (ratio *Ratio) Sub(value Ratio){
- Simplify(&value)
- ratio.num = ratio.num*value.denum - value.num*ratio.denum
- ratio.denum = ratio.denum* value.denum
- Simplify(ratio)
- }
- func (ratio *Ratio) Mul(value Ratio){
- Simplify(&value)
- ratio.num = ratio.num * value.num
- ratio.denum = ratio.denum * value.denum
- Simplify(ratio)
- }
- func (ratio *Ratio) ToFloat() float64{
- return float64(ratio.num)/float64(ratio.denum)
- }
- func Mul(lhs , rhs Ratio) Ratio{
- var res Ratio
- res.num = lhs.num * rhs.num
- res.denum = lhs.denum * rhs.denum
- Simplify(&res)
- return res
- }
- func (ratio *Ratio) Div(value Ratio){
- Simplify(&value)
- ratio.num = ratio.num* value.denum
- ratio.denum = ratio.denum* value.num
- Simplify(ratio)
- }
- func abs(ratio Ratio) Ratio{
- return Ratio{int(math.Abs(float64(ratio.num))),int(math.Abs(float64(ratio.denum)))}
- }
- func Greater(lhs Ratio, rhs Ratio) bool{
- return float64(lhs.num)/float64(lhs.denum) > float64(rhs.num)/float64(rhs.denum)
- }
- func Simplify(ratio *Ratio, is_strict ...int){
- if len(is_strict) == 0 && math.Max(math.Abs(float64(ratio.denum)),float64(ratio.num)) < 1000000{
- return
- }
- if ratio.ToFloat() == 0{
- ratio.num = 0
- ratio.denum = 1
- return
- }
- if ratio.num*ratio.denum > 0{
- if ratio.denum < 0 {
- ratio.num *= -1
- ratio.denum *= -1
- }
- }else if ratio.denum < 0{
- ratio.num *= -1
- ratio.denum *= -1
- }
- stop := math.Sqrt(math.Max(math.Abs(float64(ratio.denum)),float64(ratio.num)))
- for i := 2; float64(i) <= stop; i++{
- for ratio.num % i == 0 && ratio.denum % i == 0{
- ratio.num /= i
- ratio.denum /= i
- }
- //stop = math.Sqrt(math.Max(math.Abs(float64(ratio.denum)),float64(ratio.num)))
- }
- }
- func ToString(ratio *Ratio) string{
- Simplify(ratio,1)
- return fmt.Sprintf("%d/%d",ratio.num,ratio.denum)
- }
- func main() {
- var n int
- fmt.Scan(&n)
- B := make([]Ratio, n)
- A := make([][]Ratio, n)
- for i := 0; i < n; i++ {
- A[i] = make([]Ratio, n)
- for j := 0; j < n; j++ {
- ScanRatio(&A[i][j])
- }
- ScanRatio(&B[i])
- }
- k := 0
- max := Ratio{math.MinInt32,1}
- eps := Ratio{1,100000}
- var tmp Ratio
- for k < n{
- max = abs(A[k][k])
- index := k
- for i := k+1; i < n; i++{
- tmp = abs(A[i][k])
- if Greater(tmp,max) {
- max = abs(A[i][k])
- index = i
- }
- }
- if Greater(eps,max){
- 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 = A[i][k]
- if Greater(eps,abs(tmp)){
- continue
- }
- for j := 0; j < n; j++{
- A[i][j].Div(tmp)
- }
- B[i].Div(tmp)
- if i == k{
- continue
- }
- for j := 0; j < n; j++ {
- A[i][j].Sub(A[k][j])
- }
- B[i].Sub(B[k])
- }
- k++
- }
- var buffer stack
- for i := n-1; i >= 0; i--{
- buffer.Push(ToString(&B[i]))
- for j := 0; j < i; j++{
- B[j].Sub(Mul(A[j][i],B[i]))
- }
- }
- for !buffer.IsEmpty(){
- fmt.Println(buffer.Pop())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement