forked from davyxu/tabtoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrc2a.go
72 lines (50 loc) · 921 Bytes
/
rc2a.go
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
package util
import (
"bytes"
"fmt"
"math"
)
func mod(a, b int) int {
return int(math.Mod(float64(a), float64(b)))
}
func str2int(s string) int {
return int([]byte(s)[0])
}
var asciiA = str2int("A")
const unit = 26
// 按excel格式 R1C1格式转A1
func index2Alphabet(number int) string {
if number < 1 {
return ""
}
n := number
nl := make([]int, 0)
for {
quo := n / unit
var reminder int
x := mod(n, unit)
// 余数为0时, 要跳过这个0, 重新计算除数(影响进位)
if x == 0 {
reminder = unit
n--
quo = n / unit
} else {
reminder = x
}
nl = append(nl, reminder)
if quo == 0 {
break
}
n = quo
}
var out bytes.Buffer
for i := len(nl) - 1; i >= 0; i-- {
v := nl[i]
out.WriteString(string(v + asciiA - 1))
}
return out.String()
}
// r,c都是base1
func R1C1ToA1(r, c int) string {
return fmt.Sprintf("%s%d", index2Alphabet(c), r)
}