题解 | #字符串分隔#
字符串分隔
http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
package 牛客网.华为机试;
import java.util.Scanner;
/*
*
HJ4 字符串分隔
warning 校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
描述
•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。*/
public class StringSplitDemo {public static void main(String[] args) {
Scanner sc = new Scanner(System.in); while (sc.hasNext()){ String input = sc.nextLine(); int len = input.length(); int prefix = len % 8; int mul = len/8; StringBuilder last = new StringBuilder(input); if (prefix > 0){ for (int i = 0; i < 8 - prefix; i++) { last.append("0"); } mul ++; } for (int i = 0; i < mul ; i++) { System.out.println(last.substring(8*i,8*i +8));; } }
}
}