-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1count.go
More file actions
59 lines (48 loc) · 790 Bytes
/
1count.go
File metadata and controls
59 lines (48 loc) · 790 Bytes
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
package main
import (
"fmt"
)
func count1(n int) int {
pre := 0
count := 0
cur := 0
next := 0
i := 1
for n/i > 0 {
cur = (n / i) % 10
pre = (n / i / 10)
next = 0
if i > 1 {
next = n % i
}
if cur == 0 {
count += pre * i
} else if cur == 1 {
count = pre*i + next + 1
} else {
count += (pre + 1) * i
}
i *= 10
}
return count
}
func count(i int) int {
if i == 0 {
return 0
} else if i < 10 {
return 1
}
power := 1
for tmpI := i; tmpI > 9; tmpI = tmpI / 10 {
power *= 10
}
longest := i / power
if longest == 1 {
return count(longest*power-1) + count(i-power*longest) + i - power*longest + 1
}
return longest*count(power-1) + count(i-power*longest) + power
}
func main() {
fmt.Println(count(232))
fmt.Println(count1(232))
}