-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path10705.go
More file actions
41 lines (36 loc) · 702 Bytes
/
10705.go
File metadata and controls
41 lines (36 loc) · 702 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
// UVa 10705 - The Fun Number System
package main
import (
"fmt"
"os"
)
func solve(n int64, np string) string {
var funk string
for i := len(np) - 1; i >= 0; i-- {
lastBit := n & 1
funk = fmt.Sprint(lastBit) + funk
n /= 2
switch {
case lastBit == 1 && np[i] == 'n' && n > 0:
n++
case lastBit == 1 && np[i] == 'p' && n < 0:
n--
}
}
if n != 0 {
return "Impossible"
}
return funk
}
func main() {
in, _ := os.Open("10705.in")
defer in.Close()
out, _ := os.Create("10705.out")
defer out.Close()
var kase, k, n int64
var np string
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
fmt.Fscanf(in, "%d\n%s\n%d", &k, &np, &n)
fmt.Fprintln(out, solve(n, np))
}
}