题解 | #字符串分隔#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
- 取模: %
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = in.nextLine();
List<String> list = new ArrayList<>();
int FIXED_LEN = 8;
int len = a.length();
StringBuffer sb = new StringBuffer(a);
if (len % FIXED_LEN != 0) {
int lackZeros = FIXED_LEN - (len % FIXED_LEN);
for (int i = 0; i < lackZeros; i++) {
sb.append(0);
}
}
String afterAddZeros = sb.toString();
int afterAddZerosLen = afterAddZeros.length();
for (int i = 0; i < afterAddZerosLen / FIXED_LEN; i++) {
String str = afterAddZeros.substring(FIXED_LEN * i, FIXED_LEN * (i + 1));
list.add(str);
}
for (String s : list) {
System.out.println(s);
}
}
}

查看10道真题和解析