码迷,mamicode.com
首页 > 其他好文 > 详细

字符串分割

时间:2020-07-04 23:04:54      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:就会   temp   load   rar   pre   img   while   port   system   

技术图片

 

 

先将字符串补成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)); } } } } }

 

顺便记录一下我写的跟屎一样的玩意

import java.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));
        }
    }
}

 

字符串分割

标签:就会   temp   load   rar   pre   img   while   port   system   

原文地址:https://www.cnblogs.com/gy7777777/p/13236710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!