题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class HJ64 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while((str = br.readLine()) != null) { int n = Integer.parseInt(str); char[] ak = br.readLine().toCharArray(); //模拟翻页 int pageSize = 4; int cur = 1;//光标初始位置 Page page = new Page(pageSize, n, cur ,1, 4); for(int i = 0; i < ak.length;i++) { if('U' == ak[i]) { page.up(); }else { page.down(); } } for(int i = page.start;i <= page.end; i++) { System.out.print(i); if(i != page.end) { System.out.print(" "); } } System.out.println(); System.out.println(page.cur); } } } class Page { int pageSize; int n; int start; int end; int cur; public Page(int pageSize, int n, int cur,int start,int end) { this.n = n; this.pageSize = pageSize; this.cur = cur; this.start = start; this.end = end; } public void up() {//java没有全局变量的问题频发。 if(n < pageSize) { start = 1; end = n; if(cur == 1) { cur = n; }else{ cur-=1; } }else { if(cur == 1 && start == 1) { start = n - pageSize + 1; end = n; cur = end; // System.out.println("###1"); }else if(cur == start && start != 1){ start-=1; end-=1; cur=start; // System.out.println("###2"); }else { cur-=1; // System.out.println("###3"); } } } public void down() { if(n < pageSize) { start = 1; end = n; if(cur == n) { cur = 1; }else{ cur++; } }else { if(cur == n && end == n) { start = 1; end = start + pageSize -1; cur = start; }else if(cur == end && end != n){ start+=1; end+=1; cur=end; }else{ cur+=1; } } } }