zoukankan      html  css  js  c++  java
  • 按要求分解字符串,输入两个数M,N;M代表输入的M串字符串,N代表输出的每串字符串的位数,不够补0。例如:输入2,8, “abc” ,“123456789”,则输出为“abc00000”,“12345678“,”90000000”

     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 }
  • 相关阅读:
    超级迷宫我的计划表
    不敢死队
    Let the Balloon Rise
    Hangover
    汉诺塔系列2
    Tri Tiling(递推)
    Tiling(递推,高精度)
    Color Me Less
    I Think I Need a Houseboat(圆计算)
    Kbased Numbers(递推)
  • 原文地址:https://www.cnblogs.com/wangjiangwu/p/5830018.html
Copyright © 2011-2022 走看看