-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10427.go
60 lines (53 loc) · 907 Bytes
/
10427.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
// UVa 10427 - Naughty Sleepy Boys
package main
import (
"fmt"
"math"
"os"
"strconv"
)
var d = func() []int {
d := make([]int, 8)
nines, tens := 9, 1
for i := range d {
d[i] = (nines - tens + 1) * (i + 1)
nines = nines*10 + 9
tens *= 10
}
return d
}()
func find(n, total, idx int) byte {
from := 0
if idx > 0 {
from = int(math.Pow(10, float64(idx)))
from--
}
from += (n - total) / (idx + 1)
if (n-total)%(idx+1) > 0 {
from++
}
return strconv.Itoa(from)[(n-total-1)%(idx+1)] - '0'
}
func solve(n int) byte {
var total, idx int
for idx = range d {
if total+d[idx] >= n {
break
}
total += d[idx]
}
return find(n, total, idx)
}
func main() {
in, _ := os.Open("10427.in")
defer in.Close()
out, _ := os.Create("10427.out")
defer out.Close()
var n int
for {
if _, err := fmt.Fscanf(in, "%d", &n); err != nil {
break
}
fmt.Fprintln(out, solve(n))
}
}