首页 > 试题广场 >

MP3光标位置

[编程题]MP3光标位置
  • 热度指数:132849 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

MP3 Player因为屏幕较小,显示歌曲列表的时候每屏只能显示几首歌曲,用户要通过上下键才能浏览所有的歌曲。为了简化处理,假设每屏只能显示4首歌曲,光标初始的位置为第1首歌。

现在要实现通过上下键控制光标移动来浏览歌曲列表,控制逻辑如下:

  1. 歌曲总数<=4的时候,不需要翻页,只是挪动光标位置。

光标在第一首歌曲上时,按Up键光标挪到最后一首歌曲;光标在最后一首歌曲时,按Down键光标挪到第一首歌曲。

其他情况下用户按Up键,光标挪到上一首歌曲;用户按Down键,光标挪到下一首歌曲。

2. 歌曲总数大于4的时候(以一共有10首歌为例):


特殊翻页:屏幕显示的是第一页(即显示第1 4首)时,光标在第一首歌曲上,用户按Up键后,屏幕要显示最后一页(即显示第7-10首歌),同时光标放到最后一首歌上。同样的,屏幕显示最后一页时,光标在最后一首歌曲上,用户按Down键,屏幕要显示第一页,光标挪到第一首歌上。

一般翻页:屏幕显示的不是第一页时,光标在当前屏幕显示的第一首歌曲时,用户按Up键后,屏幕从当前歌曲的上一首开始显示,光标也挪到上一首歌曲。光标当前屏幕的最后一首歌时的Down键处理也类似。

其他情况,不用翻页,只是挪动光标就行。

数据范围:命令长度,歌曲数量
进阶:时间复杂度:,空间复杂度:

输入描述:

输入说明:
1 输入歌曲数量
2 输入命令 U或者D



输出描述:

输出说明
1 输出当前列表
2 输出当前选中歌曲

示例1

输入

10
UUUU

输出

7 8 9 10
7
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * HJ64
 * @author uyz
 * 20/09/10
 */
public class Main {
public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine()) != null) {
            int num = Integer.parseInt(str);
            // 记录歌曲索引的加减
            int index = 1;
            // 记录光标在本页移动位置
            int count = 1;
            String string = br.readLine();
            // 记录操作序列
            char[] opr = string.toCharArray();
            // 歌曲大于4时执行如下
            if (num > 4) {
                for (int i = 0; i < opr.length; i++) {
                    // 歌曲序号为1,且执行上翻操作
                    if (opr[i] == 'U' && index == 1) {
                        index = num;
                        count = 4;
                    // 序号不为1时执行上翻操作
                    } else if (opr[i] == 'U') {
                        index = index - 1;
                        count--;
                        // 光标位置小于1,仍将它置为1
                        if (count < 1) {
                            count = 1;
                        }
                    // 歌曲序号为最后且执行下翻操作
                    } else if (opr[i] == 'D' && index == num) {
                        index = 1;
                        count = 1;
                    // 序号不为最后时执行下翻操作
                    } else if (opr[i] == 'D') {
                        index = index + 1;
                        count++;
                        // 光标位置大于4,仍将它置为4
                        if (count > 4) {
                            count = 4;
                        }
                    }
                }
                if (count == 1) {
                    System.out.println(index + " " + (index + 1) + " " + (index + 2) + " " + (index + 3));
                } else if (count == 2) {
                    System.out.println((index - 1) + " " + index + " " + (index + 1) + " " + (index + 2));
                } else if (count == 3) {
                    System.out.println((index - 2) + " " + (index - 1) + " " + index + " " + (index + 1));
                } else if (count == 4) {
                    System.out.println((index - 3) + " " + (index - 2) + " " + (index - 1) + " " + index);
                }
            }
            // 歌曲小于4时执行如下
            if (num <= 4) {
                for (int i = 0; i < opr.length; i++) {
                    if (opr[i] == 'U' && index == 1) {
                        index = num;
                    } else if (opr[i] == 'U') {
                        index = index - 1;
                    } else if (opr[i] == 'D' && index == num) {
                        index = 1;
                    } else if (opr[i] == 'D') {
                        index = index + 1;
                    }
                }
                for(int i = 1; i <= num; i++){
                    System.out.print(i + " ");
                }
                System.out.println();
            }
            System.out.println(index);
        }
    }
}



编辑于 2020-09-10 17:24:34 回复(0)
class MP:
    def __init__(self,length):
        self.length = length
        self.cur = 0
        if length > 4:
            self.screen = [1,2,3,4]
        else:
            self.screen = [i for i in range(1,length+1)]
        
    def leng(self):
        if self.length > 4:
            return 4
        else:
            return self.length
        
    def up(self):
        if self.length < 4:
            self.cur = (self.cur - 1)%self.leng()
        elif self.cur > 0:
            self.cur -= 1
        else:
            if self.screen[self.cur] == 1:
                self.screen = [i for i in range(self.length+1-self.leng(),self.length+1)]
                self.cur = (self.cur - 1)%self.leng()
            else:
                self.screen = [i-1 for i in self.screen]
                
    def down(self):
        if self.length < 4:
            self.cur = (self.cur + 1)%self.leng()
        elif self.cur < self.leng()-1:
            self.cur += 1
        else:
            if self.screen[self.cur] == self.length:
                self.screen = [i for i in range(1,self.leng()+1)]
                self.cur = (self.cur + 1)%self.leng()
            else:
                self.screen = [i+1 for i in self.screen]
                
    def show_screen(self):
        if self.leng() == 1:
            print(self.screen[0])
        else:
            for i in range(self.leng()-1):
                print(self.screen[i],end=" ")
            print(self.screen[self.leng()-1])
            
    def show_index(self):
        print(self.screen[self.cur])
        
while True:
    try:
        n = int(input())
        command = list(input())
        A = MP(n)
        for com in command:
            if com == 'U':
                A.up()
            elif com == 'D':
                A.down()
        A.show_screen()
        A.show_index()
    except:
        break
发表于 2020-04-05 23:11:09 回复(1)
//设置三个变量,分别保存当前位置/顶位置/低位置
//按照逻辑来判断就行,注意顶部和底部
#include<iostream>
#include<string>

using namespace std;
int main(){
    int n;
    while(cin>>n){
        string str;
        int now=1,top,button;

        cin>>str;
        if(n<=4){
            top=1,button=n;
            for(int i=0;i<str.size();i++){
                if(str[i]=='U'){
                    if(now==1) now=button;
                    else now--;
                }
                else{
                    if(now==button) now=1;
                    else now++;
                }
            }
        }
        else{
            top=1,button=4;
            for(int i=0;i<str.size();i++){
                if(str[i]=='U'){
                    if(now==1){
                        now=n;
                        button=n;
                        top=n-3;
                    }
                    else if(now==top){
                        now--;
                        top--;
                        button--;
                    }
                    else now--;
                }
                else{
                    if(now==n){
                        top=1;
                        button=4;
                        now=1;
                    }
                    else if(now==button){
                        now++;
                        button++;
                        top++;
                    }
                    else now++;
                }
            }
        }
        for(int i=top;i<button;i++)
            cout<<i<<' ';
        cout<<button<<endl;
        cout<<now<<endl;
    }
    return 0;
}

发表于 2017-08-08 17:43:30 回复(1)
import sys

# 读取所有输入行
lines = sys.stdin.readlines()

# 步长设置为2,每次取两行做处理
for i in range(0, len(lines), 2):
    num = int(lines[i].strip())  # 获取歌曲数量
    opt = lines[i + 1].strip()  # 获取操作
    now = 1  # 初始化光标位置
    page_list = [1, 2, 3, 4] if len(opt) >= 4 else [k + 1 for k in range(num)]  # 初始化当前页
    for j in opt:

        # 光标位置判断--开始
        if j == 'U':
            now -= 1
        else:
            now += 1
        if now <= 0:  # 即判断为-1时,光标设置为结束位置
            now = num
        else:
            if now == num + 1:  # 即判断最后一首歌还点击下一首时,光标设置为开始位置
                now = 1
        # 光标位置判断--结束

        # 当前页面更新--开始
        if len(page_list) >= 4:  # 歌曲数量大于4首时,进行当前页面更新
            if now == 1:  # 光标在第一首歌时,一定是1,2,3,4
                page_list = [1, 2, 3, 4]
            elif now == num:  # 光标在最后一首歌时,一定是最后四个
                page_list = [i + 1 for i in range(num)][num - 4:]
            else:  # 其他情况,如果超过当前页面的开始位置或结束位置,对整体进行平移即可
                if now > page_list[-1]:
                    page_list = [i + 1 for i in page_list]
                if now < page_list[0]:
                    page_list = [i - 1 for i in page_list]
        # 当前页面更新--结束

    print(" ".join([str(i) for i in page_list]))
    print(now)

发表于 2021-12-15 11:43:31 回复(0)
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        int totalNum;

        while (0 != (totalNum = Convert.ToInt32(Console.ReadLine())))
        {
            int now = 1;
            int first = 1;
            if (totalNum <= 4)
            {
                foreach (var order in Console.ReadLine())
                {
                    if (now == 1 && order == 'U')
                    {
                        now = totalNum;
                    }
                    else if (now == totalNum && order == 'D')
                    {
                        now = 1;
                    }
                    else if (order == 'U')
                    {
                        now--;
                    }
                    else
                    {
                        now++;
                    }
                }
                for (int i = 1; i <= totalNum; i++)
                {
                    Console.Write(i + " ");
                }
                Console.WriteLine();
                Console.WriteLine(now);
            }
            else
            {
                foreach (var order in Console.ReadLine())
                {
                    if (now == 1 && first == 1 && order == 'U')
                    {
                        first = totalNum - 3;
                        now = totalNum;
                    }
                    else if (now == totalNum && first == (totalNum - 3) && order == 'D')
                    {
                        now = 1;
                        first = 1;
                    }
                    else if (first != 1 && now == first && order == 'U') 
                    {
                        first--;
                        now--;
                    }
                    else if (first != totalNum-3 && now == first+3 && order == 'D')
                    {
                        now++;
                        first++;
                    }
                    else if (order == 'U')
                    {
                        now--;
                    }
                    else
                    {
                        now++;
                    }
                }
                for (int i = first; i <= first+3; i++)
                {
                    Console.Write(i + " ");
                }
                Console.WriteLine();
                Console.WriteLine(now);
            }

        }
    }
}

发表于 2021-10-30 21:57:45 回复(0)
对着题目示例,认真分析情况就ok

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    string orders;
    while(cin >> n >> orders)
    {
        int first = 1;
        int sing = 1;
        
        //n <= 4
        if(n <= 4)
        {
            for(int i = 0; i < orders.size(); i++)
            {
                if(orders[i] == 'U')
                {
                    if(sing == 1) sing = n;
                    else sing -= 1;
                }
                else
                {
                    if(sing == n) sing = 1;
                    else sing += 1;
                }

            }
        }

        //n > 4 需要翻页的情况
        else
        {
            for(int i = 0; i < orders.size(); i++)
            {
                if(orders[i] == 'U')
                {
                    if(sing == 1)
                    {
                        sing = n;
                        first = n - 4 + 1;
                    }
                    else
                    {
                        sing -= 1;
                        if(sing < first)
                            first = sing;
                    }

                }

                else
                {
                    if(sing == n)
                    {
                        sing = 1;
                        first = 1;
                    }
                    else
                    {
                        sing += 1;
                        if(sing > first + 4 -1)
                            first += 1;
                    }

                }
            }
        }


        for(int i = first; i < first + 4 && i <= n; i++)
            cout << i << " ";
        cout << endl;
        cout << sing << endl;
    }
    return 0;
}
发表于 2021-07-15 17:50:06 回复(0)
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n) {
        string s;
        cin>>s;
        int index=1;
        int page=1; //当前页的起始歌曲
        int len=s.size();
        for(int i=0;i<len;++i) {
            if(s[i]=='U') {
                --index;
                if(index==0) {
                    index=n;
                    page=max(n-3,1);
                } else if(index<page) {
                    page=index;
                }
            }
            else {
                ++index;
                if(index==n+1) {
                    index=1;
                    page=1;
                } else if(index-3>page) {
                    page=index-3;
                }
            }
        }
        for(int i=page;i<=min(page+3,n);++i) {
            cout<<i<<" ";
        }
        cout<<endl;
        cout<<index<<endl;
    }
    return 0;
}

发表于 2021-06-28 20:16:52 回复(0)
#include <iostream>
#include <vector>
using namespace std;

int main() 
{
int num;
	string scmd;
	while(cin >> num)
	{
		cin >> scmd;
		vector<int> singlist;
		for (int i=1;i<=num;i++){ singlist.push_back(i); }
		
		int index=0;//选中歌曲在 singlist 中的下标
		int page_head = 0;
		if (num<=4) //4首以下
		{
			for (int j=0;j<scmd.size();j++)
			{
				if ('U'==scmd[j])
				{
					if (0==index){index = singlist.size()-1;}
					else{index--;}
				}
				else
				{
					if (singlist.size()-1==index){index = 0;}
					else{index++;}
				}
			}
		}
		else //4首以上
		{
			for (int j=0;j<scmd.size();j++)
			{
				if ('U'==scmd[j])
				{
					if (0==index){index = singlist.size()-1; page_head = index - 3;}
					else{
						if (page_head==index)
						{
							index--; 
							page_head--;
						}
						else
						{
							index--; 
						}
					}
				}
				else
				{
					if (singlist.size()-1==index){index = 0; page_head = 0;}
					else{						
						if ((page_head+3)==index)
						{
							index++;
							page_head++;
						}
						else
						{
							index++;
						}
					}
				}
			}
		}

		if (num<=4)
		{
			for (int is = 0;is<num;is++ )
			{
				cout << singlist[page_head+is] << " ";
			}	
		}
		else
		{
			for (int is = 0;is<4;is++ )
			{
				cout << singlist[page_head+is] << " ";
			}	
		}			
		cout << endl;

		cout << singlist[index] << endl;
	}
	return 0;
}

发表于 2021-04-18 19:05:59 回复(0)
自己加了点注释
import java.util.*;
public class Main{
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int num = sc.nextInt();
            String com = sc.next();
            char[] command = com.toCharArray();
            int index = 1;//坐标
            int top = 1;//当前页首
            for(char c : command){
                if(c == 'D'){
                    if(top + 3 == index){//若发生向下翻页
                        top = top+1;//页首下移一位
                    }
                    index = index + 1 ;
                    if(index == num + 1){//若是歌曲的最后一个
                        index = 1;
                        top = 1;//页首归一
                    }
                }else if(c == 'U'){
                    if(index == top){//若发生向上翻页
                         top = top - 1;//页首上移一位
                    }
                    index = index -1;
                    if(index == 0){//若是歌曲的第一个
                        index = num;
                        top = num-3;//页首为倒数第四个
                    }
                }
            }
            if(num<4){//不足四页,只有一种页面情况
                 for(int i=1;i<=num;i++){
                     System.out.print(i+" ");
                 }
            }else{//正常情况 输出页首 和 后面的4页
                 for(int i=1;i<=4;i++){
                     System.out.print(top++ +" ");
                 }
            }
            System.out.println();
            System.out.println(index);
           
        } 
     }
}


编辑于 2021-04-05 10:20:32 回复(0)
依照题意进行模拟就行,初始化光标位置为1,初始列表为1 2 3 4(如果总歌曲n小于4首就初始化为1,...,n)。翻页的时候分为以下几种情况:
(1) U操作:
i) 如果光标cur大于1,就直接自减1,但cur==list[0]的时候表示光标在顶部,U操作会使得整个列表自减1,还需要更新歌曲列表。
ii) 光标等于1,则直接cur=n跳到最后一首歌,并更新列表为最后min(4, n)首歌。
(2) D操作:
i) 如果光标cur小于n,就直接自增1,但cur==list[list.length-1]的时候表示光标在底部,D操作会使得整个列表自增1,还需要更新歌曲列表。
ii) 光标等于n,则直接cur=1跳到第一首歌,并更新列表为前min(4, n)首歌。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while((line = br.readLine()) != null){
            int n = Integer.parseInt(line);
            String op = br.readLine().trim();
            // 初始化光标位置
            int cur = 1;
            // 初始化列表
            int[] list = new int[Math.min(n, 4)];
            for(int i = 0; i < list.length; i++)
                list[i] = i + 1;
            for(int i = 0; i < op.length(); i++){
                if(op.charAt(i) == 'U'){
                    if(cur > 1){
                        if(cur != list[0]){
                            // 光标不在本页顶部,U操作不需要更新列表
                            cur --;
                        }else{
                            // 否则列表需要更新
                            cur --;
                            for(int j = 0; j < list.length; j++)
                                list[j] --;
                        }
                    }else{
                        // 光标在1,需要跳到n
                        cur = n;
                        // 歌曲列表显示最后list.length首歌
                        int temp = cur;
                        for(int j = list.length - 1; j >= 0; j--) {
                            list[j] = temp;
                            temp --;
                        }
                    }
                }else{
                    if(cur < n){
                        if(cur != list[list.length - 1]){
                            // 光标不在本页底部,D操作不需要更新列表
                            cur ++;
                        }else{
                            // 否则列表需要更新
                            cur ++;
                            for(int j = 0; j < list.length; j++)
                                list[j] ++;
                        }
                    }else{
                        // 光标在n,需要跳到1
                        cur = 1;
                        // 歌曲列表显示最开始的list.length首歌
                        int temp = cur;
                        for(int j = 0; j < list.length; j++) {
                            list[j] = temp;
                            temp ++;
                        }
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < list.length; i++)
                sb.append(list[i] + " ");
            System.out.println(sb.toString().trim());
            System.out.println(cur);
        }
    }
}

发表于 2021-03-26 14:46:10 回复(0)
很直接
def helper(n, cur, order):
    max_ = 1
    for i in order:
        if i == 'U' and cur == 1: cur = n
        elif i == 'U': cur -= 1
        elif i == 'D' and cur == n: cur = 1
        elif i == 'D': cur += 1
        if n > 4:
            if cur > max_: max_ = cur
            # 比如原先界面是7-10 max_为10 现在向上划到了5 那么max_(当前窗口最下方)就变为5+3=8
            if cur < max_ - 3: max_ = cur + 3
    return cur, max_

while True:
    try:
        n, order, cur = int(input()), input(), 1
        cur, max_ = helper(n, cur, order)
        ans = range(max_-3, max_+1) if n > 4 else range(1, n+1)
        print(" ".join(map(str, ans)))
        print(cur)
    except:
        break


发表于 2020-07-11 06:12:17 回复(0)
通过解析指令,进行移动即可,分两种情况,歌曲数目不大于4和大于4的情况。
#include <iostream>
#include <string>
using namespace std;
int main(){
    int n;        // 歌曲数量
    string str;    // 命令
    while(cin >> n >> str){
        //将n首歌曲编号1:n,pos为光标当前所在歌曲的编号,first为当前屏幕显示页的第一首歌曲的编号
        int pos = 1, first = 1;
        if(n <= 4){
            for(int i = 0; i < str.size(); ++i){
                if(str[i] == 'U' && pos == 1){    // 从首跳到尾
                    pos = n;
                }
                else if(str[i] == 'U'){
                    --pos;
                }
                else if(str[i] == 'D' && pos == n){    // 从尾跳到首
                    pos = 1;
                }
                else if(str[i] == 'D'){
                    ++pos;
                }
            }
            // 进行输出
            for(int i = 1; i < n; ++i){
                cout << i++ << ' ';
            }
            cout << n << endl << pos << endl;
        }
        else{
            for(int i = 0; i < str.size(); ++i){
                if(str[i] == 'U' && pos == 1 && first == 1){    // 从首跳到尾
                    pos = n;
                    first = n - 3;
                }
                else if(str[i] == 'U' && pos == first && first != 1){
                    --pos;
                    --first;
                }
                else if(str[i] == 'U'){
                    --pos;
                }
                else if(str[i] == 'D' && pos == n && first == n - 3){    // 从尾跳到首
                    pos = 1;
                    first = 1;
                }
                else if(str[i] == 'D' && pos == first + 3 && first != n - 3){
                    ++pos;
                    ++first;
                }
                else if(str[i] == 'D'){
                    ++pos;
                }
            }
            // 进行输出
            for(int i = first; i < first + 3; ++i){
                cout << i << ' ';
            }
            cout << first + 3 << endl << pos << endl;
        }
    }
    return 0;
}

发表于 2020-06-16 20:45:20 回复(0)
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int curPageHead = 1;//当前页头
int curPos = 1;//当前位置
int curPageRear = 4;
int pageRear = 0;//总页尾
int num = 0;
void operation(char op)
{
    if (op == 'U')
    {
        if (num < 4)//歌曲总数少于4首
        {
            if (curPos == 1)//特殊翻页
            {
                curPos = curPageRear;
            }
            else curPos--;

        }
        else
        {
            if (curPos == 1)//特殊翻页
            {
                curPos = pageRear;
                curPageRear = pageRear;
                curPageHead = pageRear - 3;

            }
            else
            {
                if (curPos == curPageHead)//光标处于页头
                {
                    //更新当前页面参数
                    curPos--;
                    curPageHead--;
                    curPageRear--;
                }
                else//
                {
                    curPos--;//只更新光标位置
                }
                
            }
        }
    }
    else if (op == 'D')
    {
        if (num < 4)
        {
            if (curPos == pageRear)//特殊翻页
            {
                curPos = 1;
            }
            else curPos++;

        }
        else
        {
            if (curPos == pageRear)//特殊翻页
            {
                curPos = 1;
                curPageRear = 4;
                curPageHead = 1;

            }
            else
            {
                if (curPos == curPageRear)//光标处于页尾
                {
                    //更新当前页面参数
                    curPos++;
                    curPageHead++;
                    curPageRear++;
                }
                else//
                {
                    curPos++;//只更新光标位置
                }

            }
        }
    }
}
int main(void)
{

    string str;
    while (cin >> num>>str)
    {
    
    //初始化页面参数
    curPos = 1;
    curPageHead = 1;
    if (num < 4)
        curPageRear = num;
    else 
        curPageRear = 4;
    pageRear = num;

    for (int i = 0; i < str.size(); i++)
    {
        operation(str[i]);
    }
    for (int j = curPageHead; j <= curPageRear; j++)
    {
        cout << j << " ";
    }
    cout << endl;
    cout << curPos << endl;

    }

    return 0;
}
发表于 2020-06-11 14:10:01 回复(0)
可以采用双循环链表进行操作
#include <stdlib.h>
typedef struct DulNode{
    int key;
    struct DulNode *next,*proir;
}DulNode,*Dulist;
 
int main(){
    int n;
    while (~scanf("%d",&n)) {
        Dulist D=(Dulist)malloc(sizeof(DulNode));
        D->next=NULL;
        D->proir=NULL;
        Dulist rear=D;
        for (int i=1; i<=n; i++) { //使用双向循环链表保存歌曲信息
            Dulist p=(Dulist)malloc(sizeof(DulNode));
            p->key=i;
            p->next=NULL;
            p->proir=NULL;
            rear->next=p;
            p->proir=rear;
            rear=rear->next;
        }
        rear->next=D;
        D->proir=rear;
        Dulist t=D->next; //指针记录当前考察歌曲
         
        char ord[1000];
        scanf("%s",ord);
        if (n>4) {
            int list[4]={1,2,3,4}; //记录当前歌曲列表歌曲序号
            int cursor=0; //初始光标指向列表内的位置
            for (int i=0; i<strlen(ord); i++) {
                switch (ord[i]) {
                    case 'U':
                        t=t->proir;
                        if (t!=D) { //当前考察歌曲没有超出列表左界限
                            if (cursor==0) { //当前光标超出左极限,需要更新列表
                                for (int j=0; j<4; j++)
                                    list[j]--;
                            }
                            else //当前光标超出左极限,只需要更新光标在列表中位置
                                cursor--;
                        }
                        else { //当前考察歌曲即将超出列表左界限,需要重置列表
                            t=t->proir;
                            for (int j=0; j<4; j++) {
                                list[j]=n-3+j;
                            }
                            cursor=3;
                        }
                        break;
                    case 'D':
                        t=t->next;
                        if (t!=D) { //当前考察歌曲没有超出列表右界限
                            if (cursor==3) { //当前光标超出右极限,需要更新列表
                                for (int j=0; j<4; j++)
                                    list[j]++;
                            }
                            else //当前光标超出右极限,只需要更新光标在列表中位置
                                cursor++;
                        }
                        else { //当前考察歌曲即将超出列表右界限,需要重置列表
                            t=t->next;
                            for (int j=0; j<4; j++) {
                                list[j]=j+1;
                            }
                            cursor=0;
                        }
                        break;
                    default:
                        break;
                }
            }
            for (int i=0; i<4; i++) {
                printf("%d",list[i]);
                if (i<3)
                    printf(" ");
            }
            printf("\n");
        }
        else {
            for (int i=0; i<strlen(ord); i++) {
                switch (ord[i]) {
                    case 'U':
                        t=t->proir;
                        if (t==D)
                            t=t->proir;
                        break;
                    case 'D':
                        t=t->next;
                        if (t==D)
                            t=t->next;
                        break;
                    default:
                        break;
                }
            }
            for (int i=0; i<n; i++) {
                printf("%d",i+1);
                if (i<n-1)
                    printf(" ");
            }
            printf("\n");
        }
        printf("%d\n",t->key);
    }
    return 0;
}

发表于 2020-05-18 18:11:27 回复(1)
#include<vector>
(721)#include<string>
#include<iostream>
(720)#include<stdlib.h>
using namespace std;

void Initialize_music_list(vector<int> &music_list,int &num)
{
	// 初始化歌曲列表

	for (int i = 0; i < num; i++)
	{
		music_list.push_back(i + 1);
	}
}

void Respond_play_commands(vector<int> &music_list, string &command_str)
{
	// 响应播放命令字符串

	int cursor = 0;  //光标定位
	int win_pos = 0; //窗口指针,即顶端歌曲索引
	int win_size = 4;
	
	// 如果歌曲列表数目小于等于4 
	if (music_list.size() <= 4)
	{
		win_size = music_list.size();
		for (int i = 0; i < command_str.size(); i++)
		{
			if (command_str[i] == 'U')
			{
				cursor == 0 ? cursor = win_size - 1 : cursor--;
			}
			if (command_str[i] == 'D')
			{
				cursor == win_size - 1 ? cursor = 0 : cursor++;
			}
		}
	}
	
	// 如果歌曲列表数目大于4 
	else
	{
		for (int i = 0; i < command_str.size(); i++)
		{
			if (command_str[i] == 'U')
			{
				if (cursor == win_pos)
				{
					// 处理窗口顶部指针
					cursor == 0 ? win_pos = music_list.size() - win_size : win_pos--;
				}
				cursor == 0 ? cursor = music_list.size() - 1 : cursor--;
			}
			if (command_str[i] == 'D')
			{
				if (cursor == win_pos + win_size - 1)
				{
					// 处理窗口顶部指针
					cursor == music_list.size() - 1 ? win_pos = 0 : win_pos++;
				}
				cursor == music_list.size() - 1 ? cursor = 0 : cursor++;
			}
		}
	}

	// 打印播放列表信息
	for (int i = win_pos; i < win_pos + win_size; i++)
	{
		cout << music_list[i] << ' ';
	}
	cout << endl;
	cout << music_list[cursor] << endl;
}


int main()
{
	int num;
	string command_str;
	while (cin >> num >> command_str)
	{	
		vector<int> music_list;
		Initialize_music_list(music_list,num);
		Respond_play_commands(music_list, command_str);
	}
	system("pause");
	return 0;
}

发表于 2020-03-09 18:32:37 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int num = sc.nextInt();
            String order = sc.next();
            int[] arr = new int[num];
            for (int i = 1; i <= num; i++) {
                arr[i-1] = i;
            }
            int display = num>=4 ? 3 : num-1;
            int tempIndex = 0;
            int begin = 0;
            int end = display;
            for (int i = 0; i < order.length(); i++) {
                if (order.charAt(i) == 'U'){
                    if (tempIndex == 0){
                        tempIndex = arr.length - 1;
                        end = tempIndex;
                        begin = tempIndex - display;
                    }else {
                        tempIndex -= 1 ;
                        if (tempIndex < begin){
                            begin = tempIndex;
                            end = begin + display;
                        }
                    }
                }else {
                    if (tempIndex == arr.length - 1){
                        tempIndex = 0;
                        begin = 0;
                        end = display;
                    }else {
                        tempIndex += 1 ;
                        if (tempIndex > end){
                            begin++;
                            end++;
                        }
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            for (int i = begin; i <= end; i++) {
                sb.append(arr[i] + " ");
            }
            System.out.println(sb.toString().trim());
            System.out.println(arr[tempIndex]);
        }
    }
}
发表于 2020-02-18 00:59:16 回复(0)
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void Move(int n,string& commd)
{
    if(n<=4)
    {
        for(int i = 1;i<=n;++i)
            cout<<i<<" ";
        cout<<endl<<commd.size()%n+1<<endl;
        return ;
    }
    vector<int> v = {1,2,3,4};
    int loct = 0;
    for(auto i = 0; i < commd.size();++i)
    {
        if(commd[i]=='U')
            loct--;
        else
            loct++;
        if(loct<0)
        {
            if(v[0]==1)
            {
                for(int i = 3; i>=0; --i)
                    v[3-i] = n-i;
                loct = 3;
            }
            else
            {
                for(int i = 0;i < 4;++i)
                    v[i]--;
                loct = 0;
            }
        }
        else if(loct>3)
        {
            if(v[3]==n)
            {
                for(int i = 0 ;i < 4;++i)
                    v[i] = i+1;
                loct = 0;
            }
            else
            {
                for(int i = 0;i < 4;++i)
                    v[i]++;
                loct = 3;
            }
        }
    }
    for(auto it = v.begin();it!=v.end();++it)
        cout<<*it<<" ";
    cout<<endl<<v[loct]<<endl;
}
int main()
{
    int n;
    string commd;
    while(cin>>n>>commd)
        Move(n,commd);
    return 0;
}

发表于 2019-11-30 19:36:20 回复(1)
//将n首歌曲编号放入playlist中,因此playlist={1,2,...n};
//屏幕上呈现的歌曲编号由win_pos 和win_size共同确定。
//cursor则是光标指定的歌曲编号的下标
//核心就是根据输入的数字和字符串确定win_pos 和win_size以及cursor.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
    int n;
    string ins;//instruction:指令
    while (cin >> n >> ins){
        int cursor = 0;//光标定位
        int win_pos = 0;//窗定位
        int win_size = 4;
        vector<int> playlist(n,0);
        for (int i = 0; i < n; i++){//初始化播放列表
            playlist[i] = i + 1;
        }
        if (n <= 4){
            win_size = n;
            for (int i = 0; i < ins.length(); i++){
                if (ins[i] == 'U'){
                    cursor == 0 ? cursor = n - 1 : cursor--;
                }
                else{
                    cursor == n - 1 ? cursor = 0 : cursor++;
                }
            }
        }
        else{
            for (int i = 0; i < ins.length(); i++){
                if (ins[i] == 'U'){
                    if (cursor == 0){
                        cursor = n - 1;//光标移到最下
                        win_pos = n - 4;//窗定位也要相应的移动
                    }
                    else{
                        if (cursor == win_pos){
                            win_pos--;//光标移动
                            cursor--;//窗定位相应的移动
                        }
                        else{
                            cursor--;//只有光标移动
                        }
                    }
                }
                else{//ins[i]=='D'
                    if (cursor == n - 1){
                        cursor = 0;//光标移到最上
                        win_pos = 0;//窗定位也要相应的移动
                    }
                    else{
                        if (cursor == win_pos + 3){
                            cursor++;//光标移动
                            win_pos++;//窗定位相应的移动
                        }
                        else{
                            cursor++;//只有光标移动
                        }
                    }
                }
            }
        }
        for (int i = win_pos; i < win_pos + win_size; i++){
            cout << playlist[i] << " ";
        }
        cout << endl;
        cout << playlist[cursor] << endl;
    }
    system("pause");
    return 0;
}

发表于 2019-07-19 11:24:04 回复(1)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int num = sc.nextInt();
            String opera = sc.next();
            int loc = 1;
            int start = 1;
            int end = 4;
            for(int i=0; i<opera.length(); i++) {
                if(opera.charAt(i) == 'U') {
                    if(loc == 1) {
                        start = num-3;
                        end = num;
                        loc = num;
                    } else {
                        if(loc == start) {
                            start --;
                            end --;
                        }
                        loc --;
                    }
                } else if(opera.charAt(i) == 'D') {
                    if(loc == num) {
                        start = 1;
                        end = 4;
                        loc = 1;
                    } else {
                        if(loc == end) {
                            start ++;
                            end ++;
                        }
                        loc ++;
                    }
                }
            }
            if(num<4) {
                int i=1;
                while(i<=num) {
                    System.out.print(i + " ");
                    i++;
                }
                System.out.println();
            } else {
                System.out.println(start+" "+(start+1)+" "+(start+2)+" "+end);
            }
            System.out.println(loc);
        }
    }
}

发表于 2018-10-07 14:13:38 回复(0)
/*看掉了初始位置为1了,之前结果总是错位……,用一个变量来保存光标,一个数组来模拟歌曲列表,
然后对每种情况进行判断,渣渣只会笨办法……*/
def getFacous(n,ins):
    temp = 1
    song = list(range(1,n+1))
    if n <= 4:
        for i in ins:
            if i == "U":
                temp -= 1
                if temp < 1:
                    temp = n
                if temp > n:
                    temp = 1
        return (song,temp)
    else:
        song = [n-3,n-2,n-1,n]
        for i in ins:
            if i == 'U':
                temp -= 1
                if temp < song[0]:
                    if temp <= 0:
                        temp = n
                        song = [n-3,n-2,n-1,n]
                    else:
                        song.insert(0,temp)
                        song.pop()
            elif i == 'D':
                temp += 1
                if temp > song[3]:
                    if temp > n:
                        temp = 1
                        song = [1,2,3,4]
                    else:
                        song.append(temp)
                        song.pop(0)
        return (song,temp)

while True:
    try:
        n = int(input())
        ins = input()
        res = getFacous(n,ins)
        song = ''
        for i in res[0]:
            song += str(i)+" "
        index = res[1]
        print(song)
        print(index)
    except:
        break

发表于 2018-09-01 19:06:01 回复(0)