题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int num = Integer.parseInt(sc.nextLine());
String str = sc.nextLine();
// System.out.println("num: " + num);
// System.out.println("str: " + str);
char[] ch = str.toCharArray();
int position = 1;//光标位置
int offset = 0;//标记光标在页内的偏移量(0-3),0-页内第一首 3-页内最后一首
//依次读取操作命令
for (int i = 0; i < ch.length; i++) {
if (ch[i] == 'U') {
if (num <= 4) {
position = Math.max(1, position - 1); //不翻页,到顶了只能停在第一首
} else {
if (position == 1) { //光标在第一首
position = num;//光标上移翻页至末尾
offset = 3;//翻页后光标在末尾
} else {
position = position - 1;//光标正常上移
offset = offset - 1 < 0 ? 0 : offset - 1; //光标在页内最顶上
}
}
// System.out.println(ch[i]+" position: "+position+" offset: "+offset);
} else if (ch[i] == 'D') {
if (num <= 4) {
position = Math.min(num, position + 1); //不翻页,到列表末尾只能在第num首
} else {
if (position == num) { //光标在最后一首
position = 1;//光标下移翻页至第一首
offset = 0;
} else {
position = position + 1;//光标正常下移
offset = offset + 1 > 3 ? 3 : offset + 1; //光标在页内最末尾
}
}
// System.out.println(ch[i]+" position: "+position+" offset: "+offset);
}
}//for循环结束
//根据光标位置和页内偏移量找到所在页起始位置
int page = 0;
int startposition = 0;//页起始位置
if (num <= 4) { //只有一页的情况
for (int m = 1; m <= num; m++) {
if ( m < num) {
System.out.print(m + " ");
} else {
System.out.println(m);
}
}
System.out.println(position);
} else {
//输出歌曲总数大于4的情况下,操作结束后的当前列表和光标位置
startposition = position - offset;
int listitem = 0;
for (int n = 1; n <= 4; n++) {
listitem = startposition + n - 1;
if (n < 4) {
System.out.print(listitem + " ");
} else {
System.out.println(listitem);
}
}
System.out.println(position);
// System.out.println(offset);
}
}//while循环结束
}
}
#华为机试题##java#public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int num = Integer.parseInt(sc.nextLine());
String str = sc.nextLine();
// System.out.println("num: " + num);
// System.out.println("str: " + str);
char[] ch = str.toCharArray();
int position = 1;//光标位置
int offset = 0;//标记光标在页内的偏移量(0-3),0-页内第一首 3-页内最后一首
//依次读取操作命令
for (int i = 0; i < ch.length; i++) {
if (ch[i] == 'U') {
if (num <= 4) {
position = Math.max(1, position - 1); //不翻页,到顶了只能停在第一首
} else {
if (position == 1) { //光标在第一首
position = num;//光标上移翻页至末尾
offset = 3;//翻页后光标在末尾
} else {
position = position - 1;//光标正常上移
offset = offset - 1 < 0 ? 0 : offset - 1; //光标在页内最顶上
}
}
// System.out.println(ch[i]+" position: "+position+" offset: "+offset);
} else if (ch[i] == 'D') {
if (num <= 4) {
position = Math.min(num, position + 1); //不翻页,到列表末尾只能在第num首
} else {
if (position == num) { //光标在最后一首
position = 1;//光标下移翻页至第一首
offset = 0;
} else {
position = position + 1;//光标正常下移
offset = offset + 1 > 3 ? 3 : offset + 1; //光标在页内最末尾
}
}
// System.out.println(ch[i]+" position: "+position+" offset: "+offset);
}
}//for循环结束
//根据光标位置和页内偏移量找到所在页起始位置
int page = 0;
int startposition = 0;//页起始位置
if (num <= 4) { //只有一页的情况
for (int m = 1; m <= num; m++) {
if ( m < num) {
System.out.print(m + " ");
} else {
System.out.println(m);
}
}
System.out.println(position);
} else {
//输出歌曲总数大于4的情况下,操作结束后的当前列表和光标位置
startposition = position - offset;
int listitem = 0;
for (int n = 1; n <= 4; n++) {
listitem = startposition + n - 1;
if (n < 4) {
System.out.print(listitem + " ");
} else {
System.out.println(listitem);
}
}
System.out.println(position);
// System.out.println(offset);
}
}//while循环结束
}
}