题解 | #MP3光标位置#

MP3光标位置

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

#include <stdio.h>
#include <string.h>
int main() {
    int n = 0;
    scanf("%d", &n);
    int arr[n+1];
    memset(arr, 0, sizeof(arr));
    for (int i = 0; i < n; i++) {
        arr[i] = i + 1;
    }
    int front = 0;
    int tail = 0;
    char word = getchar();
    word = getchar();;
    while (word!= '\n')
    {
        if (word == 'U')
        {
            if (tail == 0)
            {
                if(n>=4)
                {front = n - 4;
                tail = n - 1;}
                else {
                front=0;
                tail=n-1;
                }
            }
            else {
                tail--;
                if (tail < front)
                {
                    front--;
                }
            }
        }
        else 
        {
            if (tail == n - 1)
            {
                front = 0;
                tail = 0;
            }
            else {
                tail++;
                if (tail - front > 3)
                {
                    front++;
                }
            }
        }
        word = getchar();
    }
    if(n>=4)
    {
        for (int i = front; i < front + 4; i++)
        {
            printf("%d ", arr[i]);
        }
    }
    else {
        for (int i = front; i < n; i++)
        {
            printf("%d ", arr[i]);
        }
    }
    printf("\n");
    printf("%d\n", arr[tail]);
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务