题解 | #字符串分隔#
字符串分隔
http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
package com.niuke.test;
//题目:HJ4 字符串分隔
//描述
// •输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;
//
// •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
// 输入描述:
// 连续输入字符串(每个字符串长度小于等于100)
//
// 输出描述:
// 依次输出所有分割后的长度为8的新字符串
//示例1
// 输入:
// abc
// 输出:
// abc00000
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import static kotlin.reflect.jvm.internal.impl.builtins.StandardNames.FqNames.array;
public class Subject03 {
public static void main(String[] args) {
fun3();
}
public static void fun1(){
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int c = input.length() / 8;
int b = input.length() % 8;
int cout=0;
if (c==0) cout = c+1;
else if (c >=1 && b==0) cout=c;
else cout = c+1;
int index=0;
for (int i=0;i<cout;i++){
if ((c==0 || c==1) && b==0 ) {
System.out.print(input);
}else if (c==0 && b!=0){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(input);
for (int j=1;j<=8-input.length();j++){
stringBuilder.append("0");
}
System.out.print(stringBuilder.toString());
}else if (c>1 && b==0){
String substring = input.substring(index, index + 8);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(substring);
System.out.println(stringBuilder.toString());
} else {
if (i+1==cout){
String substring = input.substring(index, index+b);
StringBuilder stringBuilder1 = new StringBuilder();
stringBuilder1.append(substring);
for (int j=1;j<=8-b;j++) {
stringBuilder1.append("0");
}
System.out.print(stringBuilder1.toString());
}else {
String substring = input.substring(index, index + 8);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(substring);
System.out.println(stringBuilder.toString());
}
}
index+=8;
}
}
public static void fun2(){ //fun1的改良版本
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int a = input.length() % 8;
if (a==0){ //字符串长度是8的倍数
int index = 0;
for (int i=0;i<input.length()/8;i++){
String substring = input.substring(index, index + 8);
System.out.println(substring);
index +=8;
}
}else { //字符串长度不是8的倍数
if (input.length()<8){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(input);
for (int j=1;j<=8-input.length();j++){
stringBuilder.append("0");
}
System.out.print(stringBuilder.toString());
}else {
int index =0;
for (int i=0;i<=input.length()/8;i++){
if (i==input.length()/8){
StringBuilder stringBuilder = new StringBuilder();
String substrings = input.substring(index,index+input.length()%8);
stringBuilder.append(substrings);
for (int k=1;k<=8-(input.length()%8);k++){
stringBuilder.append("0");
}
System.out.println(stringBuilder.toString());
}else {
String substring = input.substring(index, index + 8);
index+=8;
System.out.println(substring);
}
}
}
}
}
public static void fun3(){
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
ArrayList<String> strings = new ArrayList<>();
for (int i=0;i<input.length();i++){
strings.add(String.valueOf(input.charAt(i)));
}
if (input.length()%8!=0){
for (int k=1;k<= 8-input.length()%8;k++){
strings.add("0");
}
}
int index=0;
for (int j=0;j<strings.size()/8;j++) {
List<String> strings1 = strings.subList(index, index + 8);
Iterator<String> iterator = strings1.iterator();
StringBuilder stringBuilder = new StringBuilder();
while (iterator.hasNext()){
String str = iterator.next();
stringBuilder.append(str);
}
System.out.println(stringBuilder.toString());
index+=8;
}
}
}