1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| package main import "fmt"
func main() { for { var a, b int _, err := fmt.Scanf("%d/%d", &a, &b) if err != nil { return }
var x = gcd(a, b) a /= x b /= x
var try bool var result []int for { if b % a == 0 && try { result = append(result, b / a) break }
var p = b/a + 1 result = append(result, p) a = a * p - b b *= p try = true }
for i, v := range result { fmt.Printf("1/%d", v) if i < len(result) - 1 { fmt.Print("+") } else { fmt.Print("\n") } }
}
}
func gcd(a, b int) int { if a == 1 || b == 1 { return 1 }
var min int if a >= b { min = b } else { min = a }
var result = 1 for i := 2; i <= min; i++ { if a % i == 0 && b % i == 0 { a /= i b /= i min /= i result *= i } }
return result }
|