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

字符串合并处理

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

import java.util.*;////一个*就好
public class Main {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            String str = scan.nextLine();////没有nextString()
            String strsp [] = str.split("\\ ");////(格式注意)\\号注意
            String strcomb = strsp[0] + strsp[1];
            char [] sarray = strcomb.toCharArray();
            for (int i = 0; i < sarray.length;
                    i = i + 2) {//0,i+2表示偶数项,选择排序
                char min = sarray[i];
                int temp = i;
                for (int j = i + 2; j < sarray.length ; j = j + 2) {

                    if (min > sarray[j]) {
                        temp = j;
                        min = sarray[j];
                    }
                }
                char tempc = sarray[i];
                sarray[i] = sarray[temp];
                sarray[temp] = tempc;
            }
            for (int i = 1; i < sarray.length; i = i + 2) {//1,i+2表示奇数项
                char min = sarray[i];
                int temp = i;
                for (int j = i + 2; j < sarray.length; j = j + 2) {
                    if (min > sarray[j]) {
                        temp = j;
                        min = sarray[j];
                    }
                }
                char tempc = sarray[i];
                sarray[i] = sarray[temp];
                sarray[temp] = tempc;
            }
            String [] scomb = new String [sarray.length];
            for (int i = 0; i < sarray.length; i = i + 1) {////这里是i+1
                if ((sarray[i] >= '0' && sarray[i] <= '9') || (sarray[i] >= 'A' &&
                        sarray[i] <= 'F') || (sarray[i] >= 'a' && sarray[i] <= 'f')) {
                    char[] tempc = Integer.toBinaryString(Integer.valueOf(("" + sarray[i]),
                                                          16)).toCharArray();//16进制转10进制,再转2进制
                    String temps = String.valueOf(tempc);////字符数组转换为String
                    while (temps.length() < 4) {
                        temps = "0" + temps;
                    }
                    tempc = temps.toCharArray();
                    for (int j = 0; j < tempc.length / 2; j++) {
                        char tempc2 = tempc[j];
                        tempc[j] = tempc[(tempc.length - j - 1)];
                        tempc[(tempc.length - j - 1)] = tempc2;
                    }
                    temps = "";
                    temps = String.valueOf(tempc);
                    scomb[i] = Integer.toHexString(Integer.valueOf(temps,
                                                   2)); //2进制转10进制,再转16进制
                    char[] tempc3 = scomb[i].toCharArray();
                    if (tempc3[0] >= 'a' && tempc3[0] <= 'f') {
                        tempc3[0] = (char)(tempc3[0] - 'a' + 'A'); ////int转char
                        scomb[i] = "" + tempc3[0];
                    }
                } else {
                    scomb[i] = "" + sarray[i];
                }
            }
            String scomb2 = "";
            for (int i = 0; i < scomb.length; i = i + 1) {
                scomb2 = scomb2 + scomb[i];
            }
            System.out.println(scomb2);
        }

    }
}
全部评论

相关推荐

勇敢的联想人前程似锦:如果我是你,身体素质好我会去参军,然后走士兵计划考研211只需要200多分。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务