暴力解决
- 输入的页数小于4的时候,另外做处理
- 大于等于4的时候,根据题目规则,详细看代码注释。
import java.util.Map;
import java.util.Scanner;
public class Main {
// 将当前页初始化为1,2,3,4
static class Page{
int one = 1;
int two = 2;
int three = 3;
int four = 4;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int pageNumber = sc.nextInt();
String s = sc.nextLine();
StringBuffer bf = new StringBuffer(sc.nextLine());
// System.out.println(bf);
int current = 1; // 初始化当前页为1
Page page = new Page();
// 下面是歌曲数目为四页以上的处理方法,如果只有一页,两页,或者三页,另外讨论
if(pageNumber >= 4){
for(int i = 0; i < bf.length();i++){
switch (bf.charAt(i)){
case 'U':// 如果是向上翻页,要判断当前页是不是顶页
if(current == 1 && page.one == 1){
page.four = pageNumber;
page.three = pageNumber - 1;
page.two = page.three - 1;
page.one = page.two - 1;
current = page.four;// 设置当前页为尾页
}else if(current == page.one){ // 如果不是第一页,但是是顶页
page.one = page.one - 1;
page.two = page.one + 1;
page.three = page.two + 1;
page.four = page.three + 1;
current = page.one;
}else{
// 当不是第一页,也不是顶页,也就是中间页,页面不需要变幻,只需要设置head,tail即可
current--;
}
break;
case 'D':
if(current == pageNumber && page.four == pageNumber){// 如果当前页是尾页,并且页面是最后一页
page.one = 1;
page.two = 2;
page.three = 3;
page.four = 4;
current = page.one;// 设置当前页为第一页
}else if(current == page.four){// 如果当前页尾尾页,那么全体+1
page.one = page.one + 1;
page.two = page.two + 1;
page.three = page.three + 1;
page.four = page.four + 1;
// 设置当前页为尾页
current = page.four;
}else{
// 如果处于中间,则直接加1即可
current++;
}
break;
}
}
System.out.println(page.one+" "+page.two+" "+page.three+" "+page.four);
System.out.println(current);
}else{
if(pageNumber == 1)// 如果只有一页,无论如何变幻都只有一页
{ System.out.println("1");
System.out.println("1");
}else if(pageNumber == 2){// 如果有两页,不需要考虑翻页的情况
int one = 1;
int two = 2;
int currentTwo = 1;
for(int i = 0; i < bf.length();i++){
switch (bf.charAt(i)) {
case 'U':
if(currentTwo == 1){
currentTwo++;
}else currentTwo--;
case 'D':
if(currentTwo == 2){
currentTwo--;
}else currentTwo++;
}
}
System.out.println("1 2");
System.out.println(currentTwo);
}else if(pageNumber == 3){
int one = 1;
int two = 2;
int three = 3;
int currentThree = 1;
for(int i = 0; i < bf.length();i++){
switch (bf.charAt(i)) {
case 'U':
if(currentThree == 1){
currentThree = 3;
}else currentThree--;
case 'D':
if(currentThree == 3){
currentThree = 1;
}else currentThree++;
}
}
System.out.println("1 2 3");
System.out.println(currentThree);
}
}
}
}
}