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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| package basic
func Add(a, b []int) []int { c := make([]int, 0) t := 0 for i := 0; i < len(a) || i < len(b); i++ { if i < len(a) { t += a[i] } if i < len(b) { t += b[i] }
c = append(c, t%10) t /= 10 } if t != 0 { c = append(c, t) } return c }
func Sub(a, b []int) []int { c := make([]int, 0) t := 0 for i := 0; i < len(a); i++ { t += a[i] if i < len(b) { t -= b[i] } c = append(c, (t+10)%10) if t < 0 { t = -1 } else { t = 0 } } for len(c) > 1 && c[len(c)-1] == 0 { c = c[:len(c)-1] } return c }
func Mul(a []int, b int) []int { c := make([]int, 0) t := 0 for i := 0; i < len(a); i++ { t = a[i]*b + t c = append(c, t%10) t /= 10 } if t > 0 { c = append(c, t) } for len(c) > 1 && c[len(c)-1] == 0 { c = c[:len(c)-1] } return c }
func Div(a []int, b int) (c []int, t int) { t = 0 for i := 0; i < len(a); i++ { t = t*10 + a[i] c = append(c, t/b) t %= b } for len(c) > 1 && c[0] == 0 { c = c[1:] } return }
|