•输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
import java.io.*; /**字符串分隔 *描述 * •输入一个字符串,请按长度为8拆分每个输入字符串并进行输出; * * •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。 * 输入描述: * 连续输入字符串(每个字符串长度小于等于100) * * 输出描述: * 依次输出所有分割后的长度为8的新字符串 * * 示例1 * 输入:abc * * 输出: abc00000 * */ // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); int strLen = line.length(); int index = 0; StringBuilder sb = new StringBuilder(); while (index < strLen) { if (index + 8 <= strLen) { // 如果剩余字符大于等于8个,直接取出8个字符并追加换行符 sb.append(line, index, index + 8).append("\n"); } else { // 如果剩余字符不足8个,取出剩余字符并补0,再追加换行符 String subStr = line.substring(index); int len_ = 8 - subStr.length(); sb.append(subStr); for (int i = 0; i < len_; i++) { sb.append("0"); } sb.append("\n"); } index += 8; } System.out.print(sb.toString()); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.nextLine(); int tirrger = 0; int length = input.length(); if (length < 8) { System.out.print(input); for (int i = 8 - length; i > 0; i--) { System.out.print("0"); } } else if (length >= 8) { for (int a = 0; a < length; a++) { if (tirrger < 8) { System.out.print(input.charAt(a)); tirrger = tirrger + 1; } else { tirrger = 1; System.out.print("\n" + input.charAt(a)); } } if (tirrger < 8) { for (int b = 0; b < 8 - tirrger; b++) { System.out.print("0"); } } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); int length1 = 8; int length2 = str.length(); if (length2 < length1) { int val = length1 - length2; for (int i = 0; i < val; i++) { str = str.concat("0"); } System.out.print(str); } else if (length2 > length1 && length2 <= 100) { float num1 = (float) 100 / length1; int num2 = Math.round(num1); int num0 = 0; for (int j = 0; j < num2; j++) { int prefix = j + num0; int suffix = prefix + length1 < 100 ? prefix + length1 : prefix + (100 - prefix); String str2 = str.substring(prefix, suffix); num0 += length1 - 1; System.out.println(str2); } } else { System.out.println("字符串长度不能超过100位!"); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String str = scanner.nextLine(); separateStr(str); } } public static void separateStr(String str) { StringBuilder sb = new StringBuilder(); sb.append(str); while (sb.length() % 8 > 0) { sb.append("0"); } String sbString = sb.toString(); while (!sbString.isEmpty()) { System.out.println(sbString.substring(0, 8)); sbString = sbString.substring(8); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String string = in.nextLine(); int currentPos = 0; for (int i = 0; i < string.length() / 8; i++) { System.out.println(string.substring(currentPos,currentPos + 8)); currentPos = currentPos + 8; } if (string.length() % 8 > 0) { StringBuilder sb = new StringBuilder("00000000"); sb.replace(0,string.length() % 8, string.substring(currentPos)); System.out.println(sb); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); if (s.isEmpty()) { System.out.println(); return; } int cur = 0; int seg = s.length() / 8; int lack = s.length() % 8; for (int i = 0; i < seg; i++) { System.out.println(s.substring(i * 8, (i + 1) * 8)); } if (lack != 0) { int needZero = 8 - lack; String t = s.substring(s.length()- lack, s.length()); StringBuilder sb = new StringBuilder(); for (int k = 0; k < needZero; k++) { sb.append("0"); } System.out.println(t + sb.toString()); } } }
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(); // 初始化将要打印的字符串 String initStr = "00000000"; char[] charArray = line.toCharArray(); // 预期结果 char[] res = initStr.toCharArray(); for (int i = 0; i < charArray.length; i++) { // 每8个重置1次 if (i % 8 == 0) { String str = new String(res); // 首次跳过,(因为0%8=0) if (!initStr.equals(str)) { System.out.println(str); res = initStr.toCharArray(); } } // 将0替换掉 res[i % 8] = charArray[i]; } // 打印最终的剩余 String str = new String(res); if (!initStr.equals(str)) { System.out.println(new String(res)); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String str = in.nextLine(); test(str); } } private static void test(String s) { if (s == null || s.length() == 0) return; char[] arr = new char[8]; for (int i = 0; i < s.length(); i++) { int index = (i + 1) % 8; if (index == 0) { arr[7] = s.charAt(i); System.out.println(new String(arr)); arr = new char[8]; } else arr[index - 1] = s.charAt(i); if (index != 0 && i == s.length() - 1) { for (int j = 0; j < arr.length; j++) { if (arr[j] == '\0') arr[j] = '0'; } System.out.println(new String(arr)); } } } }
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); } } } }