题解 | #字符串分隔#
字符串分隔
http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
import java.util.Scanner;
import java.util.TreeSet;
/**
* @ProjectName: leetcode
* @Package: com.company.niuke
* @ClassName: Main
* @Author: Administrator
* @Description: 牛客网测试
* @Date: 2022/2/11 20:30
* @Version: 1.0
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
StringBuilder str = new StringBuilder(scanner.nextLine());
if (str.length() <= 8) { //字符长度小于等于8就进行拼接
append(str);
} else { //字符长度大于8就进行分割
subString(str.toString());
}
}
}
private static void append(StringBuilder str) {
int length = str.length(); //保存字符的原有长度,决定了要拼接几个0;
for (int i = 0; i < 8 - length; i++) {
str.append('0');
}
System.out.println(str); //直接输出
}
private static void subString(String str) {
System.out.println(str.substring(0, 8)); //输出前8个字符
if (str.length()-8 > 8) { //如果输出8个字符之后,剩余字符的长度依然大于8,则继续分割
subString(str.substring(8, str.length()));
} else { //否则则进行拼接
append(new StringBuilder(str.substring(8, str.length())));
}
}
}