题解 | #字符串分隔#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
public static void main(String[] args){ Scanner in = new Scanner(System.in); String str = in.nextLine(); int k = str.length() % 8; //获取余数 if(k != 0){ //判断余数是否为0,如果不加判断的话,在for循环内会加8个"0",此时已经包含了字符串为空的情况,不用单独判断 for(int i = 0; i < 8-k; i++){ //将"0"添加到字符串当中 str = str + "0"; } } for(int j = 0; j + 7 < str.length(); j = j + 8){ //每8个为一组输出字符串 System.out.println(str.substring(j, j + 8)); } }
思路:
1.首先判断需要补几个"0"(特殊情况,余数为0的情况下不用补"0");
2.将补的"0"添加到输入的字符串当中;
3.每8个为一组输出字符串。
#华为机试##你的秋招进展怎么样了#