字符串分割 Java解法 11ms
使用BufferedReader按行读取字符串;int patchLen = 8 - s.length() % 8;
计算需要补足的位数,若为0,则说明不需要补足;
按8个一组,依次读取字符串每一个字符,内部循环完成时,检查当前stringBuilder是否有8位,如果不足则补齐;
输出字符串并清空stringBuilder
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = ""; while ((s = br.readLine()) != null) { int patchLen = 8 - s.length() % 8; StringBuilder sb2 = new StringBuilder(); for (int i = 0; i < s.length(); i = i + 8) { for (int j = i; j < i + 8 && j < s.length(); j++) { sb2.append(s.charAt(j)); } if (sb2.toString().length() != 8) { for (int m = 0; m < patchLen; m++) { sb2.append('0'); } } System.out.println(sb2.toString()); sb2.setLength(0); } } } }