题解 | #字符串合并处理#

字符串合并处理

https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String s = scanner.next();
            String s2 = scanner.next();
            s=s+s2;
//            System.out.println(s);
            char[] cs = s.toCharArray();
            ArrayList<Character> odd = new ArrayList<>();
            ArrayList<Character> even = new ArrayList<>();
            String odds="";
            String evens="";
            for (int i = 0; i <cs.length; i++) {
                if(i%2==0){
                    even.add(cs[i]);
                    evens=evens+cs[i];
                }else {
                    odd.add(cs[i]);
                    odds=odds+cs[i];
                }
            }
            char[] cbackodd = cback(odds);
            char[] cbackeven = cback(evens);
            int countod=0;
            int countev=0;
            String re="";
            for (int i = 0; i <cs.length; i++) {
                if(i%2==0){
                    re=re+cbackeven[countod];
                    countod++;
                }else {
                    re=re+cbackodd[countev];
                    countev++;
                }
            }
//            System.out.println(re);
            String reback = Main.reback(re);
            System.out.println(reback);
        }
    }
    public static char[] cback(String str){
        char[] chars= str.toCharArray();
        Arrays.sort(chars);
        return chars;
    }
    public static String reback(String str){
        String[] split = str.split("");
        for (int i = 0; i < split.length; i++) {
            if (split[i].matches("[a-fA-F0-9]")){
                int i1 = Integer.parseInt(split[i], 16);//16进制转10进制
                String strer = Integer.toString(i1, 2);
                int length = strer.length();
                ArrayList<Integer> inte= new ArrayList<>();
                inte.add(length);
                if (strer.length()<5){
                    for (int j = 0; j <4-inte.get(0); j++) {
                        strer="0"+strer;
                    }
                }
//                System.out.println("未反转二进制数为"+strer);
                String newer="";
                for (int j = strer.length()-1; j >=0; j--) {
                    newer=newer+strer.charAt(j);
                }
//                System.out.println("反转后二进制数为"+newer);
                int i2 = Integer.parseInt(newer, 2);//2进制转10进制
                String string = Integer.toString(i2, 16);// 十进制整数10转成16进制
                char c = string.charAt(0);
                if (Character.isLowerCase(c)){
                    string = String.valueOf(c).toUpperCase();
                }
                split[i]=string;
            }
        }
//        System.out.println("加密数组为"+Arrays.toString(split));
        String success="";
        for (int i = 0; i < split.length; i++) {
            success=success+split[i];
        }
        return success;
    }
}

全部评论

相关推荐

vegetable_more_exercise:1-1.5万,没错啊,最少是1人民币,在区间内
点赞 评论 收藏
分享
10-25 23:12
门头沟学院 Java
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务