题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
package main
import(
"fmt"
"bufio"
"os"
"strconv"
)
func min(a, b int)int{
if a < b{
return a
}
return b
}
func max(a, b int)int{
if a < b{
return b
}
return a
}
func main(){
reader := bufio.NewReader(os.Stdin)
temp1, _ := reader.ReadString('\n')
temp2, _ := reader.ReadString('\n')
n, _ := strconv.Atoi(temp1[:len(temp1) - 1])
str := temp2[:len(temp2) - 1]
left, right := 0, 0
if n < 4{
left, right = 1, n
}else{
left, right = 1, 4
}
cur := 1
for i := 0; i < len(str); i++{
if str[i] == 'U'{
if cur > left{
cur--
}else if cur == left{
if left == 1{
cur = n
left, right = max(n + 1 - 4, 1), n
}else{
cur--
left--
right--
}
}
}else{
if cur < right{
cur++
}else if cur == right{
if right == n{
cur = 1
left, right = 1, min(4, n)
}else{
cur++
left++
right++
}
}
}
}
for i := left; i <= right; i++{
fmt.Printf("%d ", i)
}
fmt.Println()
fmt.Println(cur)
}
import(
"fmt"
"bufio"
"os"
"strconv"
)
func min(a, b int)int{
if a < b{
return a
}
return b
}
func max(a, b int)int{
if a < b{
return b
}
return a
}
func main(){
reader := bufio.NewReader(os.Stdin)
temp1, _ := reader.ReadString('\n')
temp2, _ := reader.ReadString('\n')
n, _ := strconv.Atoi(temp1[:len(temp1) - 1])
str := temp2[:len(temp2) - 1]
left, right := 0, 0
if n < 4{
left, right = 1, n
}else{
left, right = 1, 4
}
cur := 1
for i := 0; i < len(str); i++{
if str[i] == 'U'{
if cur > left{
cur--
}else if cur == left{
if left == 1{
cur = n
left, right = max(n + 1 - 4, 1), n
}else{
cur--
left--
right--
}
}
}else{
if cur < right{
cur++
}else if cur == right{
if right == n{
cur = 1
left, right = 1, min(4, n)
}else{
cur++
left++
right++
}
}
}
}
for i := left; i <= right; i++{
fmt.Printf("%d ", i)
}
fmt.Println()
fmt.Println(cur)
}