字串分割
阿新 • • 發佈:2020-07-05
先將字串補成8的倍數,然後再分割字串,列印輸出
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
//我也不知道為什麼要用while迴圈,沒有迴圈牛客就會報錯,說沒有迴圈輸入 while(sc.hasNext()) { int n = sc.nextInt(); for(int i=0; i<n; i++) { String temp= sc.next(); while(temp.length()%8!=0) { temp += "0"; } for(int j=0; j<temp.length(); j+=8) { System.out.println(temp.substring(j, j+8)); } } } } }
順便記錄一下我寫的跟屎一樣的玩意
importjava.util.*; import java.lang.*; public class Main{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String num = sc.nextLine(); int n = Integer.valueOf(num); String[] str = new String[n];for (int i = 0; i < n; i++) { str[i] = sc.nextLine(); if (str[i].length() < 8) { str[i] = ArrCmp(str[i]); System.out.println(str[i]); } else if (str[i].length() > 8) { int count = str[i].length() / 8; int r = str[i].length() % 8; if (r == 0) { show(str[i], count); } else { String pre = str[i].substring(0, count * 8); show(pre, count); String sub = str[i].substring(count * 8); sub = ArrCmp(sub); System.out.println(sub); } } else { System.out.println(str[i]); } } } } public static String ArrCmp(String str) { char[] ss = str.toCharArray(); char[] res = new char[8]; int len = str.length(); for (int i = 0; i < len; i++) { res[i] = ss[i]; } for (int i = len; i < 8; i++) { res[i] = '0'; } return new String(res); } public static void show(String str, int count) { char[] ss = str.toCharArray(); char[] res = new char[8]; for (int i = 0; i < count; i++) { for (int j = 0; j < 8; j++) { res[j] = ss[i * 8 + j]; } System.out.println(new String(res)); } } }