题解 | #字符串分隔#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
import java.util.Scanner; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String str = sc.nextLine(); StringBuilder sb = new StringBuilder();//牢记字符串缓冲区的建立语法//可变的StringBuilder sb.append(str);//字符串缓冲区的加入 int size = str.length(); //这个是先填充 后裁剪 int addZero = 8 - size % 8; //addzero的可能值包括8 while ((addZero > 0) && (addZero < 8)) { //注意边界调节,避免addzero=8 sb.append("0");//使用‘’或“”都可 addZero--; } String str1 = sb.toString();//转换成String //这个是用了substring裁剪,来使得代码更加稳定//我之前的思路是8i,8(i+1) while (str1.length() > 0) { System.out.println(str1.substring(0, 8)); str1 = str1.substring(8); } } } }