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

字符串合并处理

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

import java.util.; import java.io.;

public class Main{

 public static void main(String[] args){

    InputStreamReader reader =  new InputStreamReader(System.in);
    try(BufferedReader br = new BufferedReader(reader)){

        String[] strs = br.readLine().split(" ");
        String str_concat =  strs[0].concat(strs[1]);

        StringBuffer sb_odd =  new StringBuffer();
        StringBuffer sb_even =  new StringBuffer();
        StringBuffer sb_result =  new StringBuffer();


        for(int i=0;i<str_concat.length();i++){

            if(i%2==0)
                sb_odd.append(str_concat,i,i+1);
            else
                sb_even.append(str_concat,i,i+1);
        }

        String str_odd = sb_odd.toString();
        String str_even = sb_even.toString();

        str_odd = sort(str_odd.toCharArray());
        str_even = sort(str_even.toCharArray());


        // 奇数位最少等于偶数位
        for (int i =0; i <str_odd.length(); i++) {


            sb_result.append(str_odd, i, i+1);

            if ((i == str_odd.length()-1) && str_odd.length() > sb_even.length()) continue;

            sb_result.append(str_even, i, i+1);

        }
        System.out.println(translate(sb_result.toString().toCharArray()));



    }catch(IOException e){
        System.out.println(e.getMessage());
    }
}

public static String sort(char[] chars){

    char tmp = ' ';
    for (int i = 0; i < chars.length-1; i++) {
        for (int j = 0; j < chars.length-1; j++) {

            if (chars[j]>chars[j+1]){
                tmp = chars[j+1];
                chars[j+1]=chars[j];
                chars[j] = tmp;
            }

        }
    }

    return String.valueOf(chars);

}

public static String translate(char[] chars){


    StringBuffer buffer_result = new StringBuffer();
    for (int i = 0; i < chars.length; i++) {

        StringBuffer buffer = new StringBuffer();
        int digit = 0;
        if (Character.isLetter(chars[i])){
            //不是 十六进制字母
            if (Character.toLowerCase(chars[i]) > 'f' ||Character.toLowerCase(chars[i])< 'a' ){
                buffer_result.append(chars[i]);
                continue;
            }
             digit = Character.digit(chars[i], 16);
        }

        if (Character.isDigit(chars[i])){
             digit = Character.digit(chars[i], 10);
        }

        String binaryString = Integer.toBinaryString(digit);

        while (binaryString.length()!=4){
            binaryString = "0"+binaryString;
        }

        StringBuffer reverse_binaryString = buffer.append(binaryString).reverse();

        int reverse_value = Integer.parseInt(reverse_binaryString.toString(), 2);

        if (reverse_value<=9){
            buffer_result.append(reverse_value);
        }else {
            char tmp = ' ';
            switch (reverse_value) {
                case 10: tmp = 'A';
                break;
                case 11: tmp = 'B';
                    break;
                case 12: tmp = 'C';
                    break;
                case 13: tmp = 'D';
                    break;
                case 14: tmp = 'E';
                    break;
                case 15: tmp = 'F';
                    break;
            }
            buffer_result.append(tmp);
        }
    }
return buffer_result.toString();
}

}

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-21 19:05
点赞 评论 收藏
分享
尊尼获获:闺蜜在哪?
点赞 评论 收藏
分享
牛客868257804号:九个中铁八个中建
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务