题解 | #字符串分隔#
字符串分隔
http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str =input.nextLine(); int n = str.length(); // System.out.println(n);
//判断字符串有几个8的倍数
int beishu=n/8;
//判断是否有余数
int yushu =n%8;
//减去余数应添加的0个数
int add0=8-yushu;
if (yushu!=0&&str.length()<8){ //字符串大小小于8
yushu=8-str.length();
add0=8-str.length();
String str2[] = new String[1];
str2[0]=str;
for (int i = 0; i <add0 ; i++) {
str2[0]=str2[0]+"0";
}
System.out.println(str2[0]);
}else if (yushu==0){ //字符串大小为8的倍数
String str2[]= new String[beishu];
for (int i = 0; i <str2.length ; i++) {
str2[i]="";
}
for (int i = 0; i <str2.length; i++) {
for (int j = i*8; j <i*8+8; j++) {
str2[i]=str2[i]+str.charAt(j);
}
}
for (int i = 0; i <str2.length ; i++) {
System.out.println(str2[i]);
}
}else { //字符串大于8且大小不为8的倍数
String str2[]= new String[beishu+1];
for (int i = 0; i <str2.length ; i++) {
str2[i]="";
}
for (int i = 0; i <str2.length-1; i++) {
for (int j = i*8; j <i*8+8; j++)
str2[i] = str2[i] + str.charAt(j);
}
//补充剩下的字符
for (int i = 8*beishu; i <str.length(); i++) {
str2[str2.length-1]=str2[str2.length-1]+str.charAt(i);
}
//添加0
for (int i = 0; i <add0 ; i++) {
str2[str2.length-1]=str2[str2.length-1]+"0";
}
for (int i = 0; i <str2.length ; i++) {
System.out.println(str2[i]);
}
}
}
}