题解 | #字符串分隔#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
1、根据与8之间的余数首先补0保证 长度 是8的倍数,再通过输出控制8个一组
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); int len = str.length(); //字符串长度 int size = 8 - (len % 8); //所需补0 长度 // String newstr = str + "0" * size; if(size != 0 && size != 8){ //要考虑到 8和0 两种情况 for(int i = 0; i < size; i++){ str += "0"; } } int count = 0; //在输出上控制8个一输出 for(int i = 0; i < str.length(); i++){ System.out.print(str.charAt(i)); count++; if(count == 8){ System.out.println(); count = 0; //保证8个一重复 } } } }