1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Text { 5 6 @SuppressWarnings("resource") 7 public static void main(String[] args) { 8 Scanner scanner = new Scanner(System.in); 9 int M = scanner.nextInt();//M代表输入的M串字符串 10 int N=scanner.nextInt();//N代表输出的每串字符串的位数 11 ArrayList<String> arrayList = new ArrayList<String>(); 12 ArrayList<String> arrayList1 = new ArrayList<String>(); 13 String[] arr=new String[M] ; 14 String a=scanner.next(); 15 arr=a.split(","); 16 for (int i = 0; i < arr.length; i++) { 17 arrayList.add(arr[i]); 18 } 19 for (int i = 0; i < arrayList.size(); i++) { 20 if (arrayList.get(i).length() < N) { 21 String temp0="" ; 22 for (int j = 0; j < N - arrayList.get(i).length(); j++) { 23 temp0 = temp0 + "0"; 24 } 25 arrayList1.add(arrayList.get(i) + temp0); 26 } 27 if (arrayList.get(i).length() >N) { 28 String temp=arrayList.get(i); 29 int t=(int) Math.ceil((double)temp.length()/N); 30 for (int j = 0; j < t; j++) { 31 if (j==t-1) { 32 if ((temp.substring(N*j)).length()<N) { 33 String temp1 = ""; 34 for (int s = 0; s < N - (temp.substring(N*j)).length(); s++) { 35 temp1 = temp1 + "0"; 36 } 37 arrayList1.add(temp.substring(N*j)+temp1); 38 }else { 39 40 arrayList1.add(temp.substring(N*j)); 41 } 42 }else { 43 arrayList1.add(temp.substring(N*j, N*(j+1))); 44 } 45 46 } 47 } 48 } 49 50 for (String string : arrayList1) { 51 System.out.print(string+" "); 52 } 53 } 54 55 }
原文:http://www.cnblogs.com/wangjiangwu/p/5830018.html