MP3光标位置

MP3光标位置--牛客华为机试-64

原题链接

package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
)

func main() {
    input := bufio.NewScanner(os.Stdin)
    for input.Scan() {
        num, _ := strconv.Atoi(input.Text()) //数量
        data := make([]int, 0)
        for i := 1; i <= num; i++ {  //构造序号数组
            data = append(data, i)
        }
        input.Scan()
        str := input.Text() //指令
        start := 0          //显示起始位置
        index := 0          //光标所在位置
        for _, v := range str {
            if v == 'U' {
                if index == 0 { //轮回翻页
                    start = num - 4
                    index = index + num - 1
                } else {
                    index--
                    if index < start { //常规翻页
                        start = index 
                    }
                }
            } else if v == 'D' {
                if index == num-1 { //轮回翻页
                    index = 0
                    start = 0
                } else {
                    index++
                    if index > start+3 { //常规翻页
                        start = start + 1
                    }
                }
            }
        }
        resStr := ""
        if num <= 4 {
            for _, v := range data {
                resStr += fmt.Sprintf("%d ", v)
            }
        } else {
            for _, v := range data[start : start+4] {
                resStr += fmt.Sprintf("%d ", v)
            }
        }
        fmt.Println(resStr)
        fmt.Println(data[index])
    }
}

 

posted @ 2023-02-22 02:00  $wanggang  阅读(13)  评论(0编辑  收藏  举报