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
|
package main
import (
"fmt"
"math"
)
func getNthDigit(num, k int) int {
result := num
if k-1 > 0 {
result -= num - (num/int(math.Pow10(k-1)))*int(math.Pow10(k-1))
}
if math.Pow10(k) <= float64(num) {
result -= (num / int(math.Pow10(k))) * int(math.Pow10(k))
}
// result := num - (num/int(math.Pow10(k)))*int(math.Pow10(k)) - (num/int(math.Pow10(k-2)))*int(math.Pow10(k-2))
result /= int(math.Pow10(k - 1))
// fmt.Println(num, k, result)
return result
}
func addToArrayForm(num []int, k int) []int {
length := int(math.Max(float64(len(num))+1., 5.))
result := make([]int, length)
carry := false
num_index := len(num) - 1
var sum int
for i := length - 1; i >= 0; i-- {
if num_index >= 0 {
sum = num[num_index] + getNthDigit(k, (length-i))
} else {
sum = getNthDigit(k, (length - i))
}
if carry {
sum++
}
if sum >= 10 {
carry = true
} else {
carry = false
}
result[i] = sum % 10
num_index--
}
if carry {
result[0] = 1
}
for j := 0; j < length; j++ {
if result[j] != 0 {
return result[j:]
}
}
return result
}
func main() {
fmt.Println(addToArrayForm([]int{1, 2, 0, 0}, 34))
fmt.Println(addToArrayForm([]int{2, 7, 4}, 181))
fmt.Println(addToArrayForm([]int{2, 1, 5}, 806))
fmt.Println(addToArrayForm([]int{0}, 806))
}
|