题解 | #字符串分隔#
字符串分隔
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);
String inputStr = in.next();
int strLen = inputStr.length();
int reLen = strLen%8 == 0 ? strLen/8 : strLen/8 +1;
String[] result = new String[reLen];
for(int i = 0; i < reLen; i++ ){
if(reLen * 8 > inputStr.length() && i == reLen - 1 ){ // 最后一个串长度不满足8
String tmpLastStr = inputStr.substring(i*8 ,inputStr.length());
for(int j = 0 ; j < ( reLen * 8 ) - inputStr.length(); j++){
tmpLastStr = tmpLastStr.concat("0");
}
result[i] = tmpLastStr;
} else {
result[i] = inputStr.substring(i*8 ,(i+1)*8);
}
}
for(String out : result){
System.out.println(out);
}
}
}
查看21道真题和解析