题解 | #MP3光标位置#

MP3光标位置

https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15

import java.util.Scanner

fun main(args: Array<String>) {
   val read = Scanner(System.`in`)
    val res = read.nextLine().toInt()
    val ops = read.nextLine()

    var cur = 1
    var pageFirst = 1
    var page = if(res > 4) 4 else res
    var pageLast = page
    ops.forEach {
        when (it) {
            'U' -> {
                cur -= 1
                if (cur == 0) {
                    cur = res
                    pageFirst = res - (page - 1)
                    pageLast = res
                } else {
                    if (cur < pageFirst) {
                        pageFirst = cur
                        pageLast--
                    }
                }
//                println("$cur $pageFirst $pageLast")
            }

            'D' -> {
                cur += 1
                if (cur > res) {
                    cur = 1
                    pageFirst = 1
                    pageLast = 4
                } else {
                    if (cur > pageLast) {
                        pageLast = cur
                        pageFirst++
                    }
                }
//                println("$cur $pageFirst $pageLast")
            }
        }
    }

    for (i in 0 until page) {
        print("${i + pageFirst} ")
    }
    println()
    print(cur)
}

全部评论

相关推荐

威猛的小饼干正在背八股:挂到根本不想整理
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务