•输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); // 填充倍数的算法 String fillString = fillingMultiple(str, 8); for (int i = 0; i < fillString.length(); i = i + 8) { System.out.println(fillString.substring(i, i + 8)); } } public static String fillingMultiple(String str, int multiple){ int remainder = str.length() % multiple; if(remainder != 0){ int fillZero = multiple - remainder; for(int i = 0; i < fillZero; i++){ str += "0"; } } return str; } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String str = in.nextLine(); int length = str.length(); // 长度为0就返回 if (length == 0) { return; } int n = length / 8; // 补全的0字符串 String mm = "00000000".substring(length % 8); for (int i = 0; i <= n; i++) { if (i == n) { if (length % 8 == 0) return; System.out.println(str.substring(8 * i) + mm); } else { System.out.println(str.substring(8 * i, (i + 1) * 8)); } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String a = in.nextLine(); int len = a.length(); int s = len % 8; int t = len / 8; if (s == 0) { /*是8的倍数分开输出 */ for (int i = 0; i < len; i++) { System.out.print(a.charAt(i)); if((i+1) % 8 == 0 && i!=0) { System.out.println(); } } } else { /*不是8 */ for (int i = 0; i < len; i++) { System.out.print(a.charAt(i)); if((i+1) % 8 == 0 && i!=0) { System.out.println(); } } for(int j = 0; j < 8 - s; j++){ System.out.print(0); } } } }
public class PracticeTest { public static void main(String[] args){ Scanner in = new Scanner(System.in); StringBuilder str = new StringBuilder(in.nextLine()); int count = (int)Math.ceil((double)str.length() / 8); int left = str.length() % 8; if (str.length() < 8) { str.append("0".repeat((8 - left))); System.out.println(str); } else { if (left > 0) { for (int i = 0; i < count - 1; i++) { System.out.println(str.substring(i * 8, (i + 1) * 8)); } System.out.println(str.substring(str.length() - left) + "0".repeat(left)); } else { for (int i = 0; i < count; i++) { System.out.println(str.substring(i * 8, (i + 1) * 8)); } } } } }
写复杂了 import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void solution(String inputStr) { if(inputStr.length() >= 8) { int index = 0; int endIndex = 8; int length = inputStr.length(); while(index <= length - 1) { String nStr; if(endIndex <= length - 1) { nStr = inputStr.substring(index, endIndex); System.out.println(nStr); } else { nStr = inputStr.substring(index, length); if(nStr.length() < 8) { StringBuilder sb = new StringBuilder(); sb.append(nStr); sb.append("0"); while(sb.length() < 8) { sb.append("0"); } System.out.println(sb.toString()); } else { System.out.println(nStr.toString()); } } index = index + 8; endIndex = endIndex + 8; } } else { StringBuilder sb = new StringBuilder(); sb.append(inputStr); sb.append("0"); while(sb.length() < 8) { sb.append("0"); } System.out.println(sb.toString()); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String inputStr = in.nextLine(); solution(inputStr); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String line = in.nextLine(); // 注意 hasNext 和 hasNextLine 的区别 int n=line.length()/8; int m=line.length()%8; int start=0; for(int i=0;i<n;i++){ System.out.println(line.substring(start,start+8)); start+=8; } if(m!=0) { StringBuffer sb= new StringBuffer(); String substring = line.substring(line.length() - m, line.length()); sb.append(substring); for(int i=0;i<8-m;i++){ sb.append("0"); } System.out.println(sb.toString()); } } }
import java.util.Scanner; public class hj4 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); int strLength = str.length(); for (int i = 0; i < strLength;) { if((strLength-i)/8 > 0){ System.out.println(str.substring(i,i+8)); i = i+8; }else{ System.out.println(str.substring(i,strLength)+"00000000".substring(0,8-(strLength-i))); break; } } } }
import java.util.Scanner; import static java.lang.Math.min; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String str = scanner.nextLine(); int length = str.getBytes().length; int endIndex = 0; int beginIndex = 0; while (endIndex < length) { beginIndex = endIndex; endIndex = min(length, endIndex + 8); String resStr = str.substring(beginIndex, endIndex); while (resStr.getBytes().length != 8) { resStr = resStr + "0"; } System.out.println(resStr); } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); str += "0000000".substring(0, str.length()%8 == 0 ? 0 : 8 - str.length() % 8); for(int i = 0; i < str.length(); i = i + 8){ System.out.println(str.subSequence(i, i+8)); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String data = sc.nextLine(); int addCount =data.length() % 8; StringBuilder sb = new StringBuilder(data); if(addCount != 0){ for(int i = 0;i<8-addCount;i++){ sb.append("0"); } } String tgtStr = sb.toString(); for(int i = 0; i<tgtStr.length();i=i+8){ System.out.println(tgtStr.substring(i,i+8)); } } }