题解 | 字符串分隔
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String input = in.nextLine();
if (input.length() < 8 && input.length() > 0) {
leOut(input);
} else {
gteOut(input);
}
}
}
private static void leOut(String input) {
System.out.println(input + zereStr(8 - input.length()));
}
private static void gteOut(String input) {
int subNum = input.length() % 8 == 0 ? input.length() / 8 : input.length() / 8 + 1;
String out = "";
for (int j = 0 ; j < subNum ; j++) {
if (j == subNum -1 ) {
out = input.substring(j * 8, input.length());
System.out.println(out + zereStr(8 - out.length()));
} else {
System.out.println(input.substring(j * 8, j * 8 + 8));
}
}
}
public static String zereStr(int num) {
String zereStr = "";
for (int i = 0 ; i < num ; i++) {
zereStr += "0";
}
return zereStr;
}
}