rambless

字符串合并处理

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str1 = in.next();
            String str2 = in.next();
            String str = str1 + str2;
            char[] arr = str.toCharArray();
            String a = "";
            String b = "";
            for (int i = 0; i < arr.length; i++) {
                if (i % 2 == 0) {
                    a += arr[i];
                } else {
                    b += arr[i];
                }
            }
            char[] arr1 = a.toCharArray();
            char[] arr2 = b.toCharArray();
            Arrays.sort(arr1);
            Arrays.sort(arr2);
            String temp = "";
            int max = Math.max(arr1.length, arr2.length);
            for (int i = 0; i < max; i++) {
                if (i < arr1.length) {
                    temp += arr1[i];
                }
                if (i < arr2.length) {
                    temp += arr2[i];
                }
            }
            stepThree(temp);
        }
    }

    private static void stepThree(String temp) {
        char[] arr = temp.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            //8084842CAE9B9G7D7BUFooqqrrrvuxu
            if (Character.isDigit(arr[i])) {
                dealDigit(arr[i]);
            } else {
                dealChar(arr[i]);
            }
        }
    }

    private static void dealDigit(char c) {
        Integer num = Integer.parseInt(c + "");
        String temp = "";
        if (num == 1) {
            temp = "1000";
        } else {
            while (num > 0) {
                temp += num % 2;
                num = num / 2;
            }
            if (temp.length() < 4) {
                int len = 4 - temp.length();
                while (len > 0) {
                    temp += "0";
                    len--;
                }
            }
        }
        cal(temp);
    }

    private static void dealChar(char t) {
        char c = Character.toLowerCase(t);
        String str;
        if (c == 'a') {
            str = revese("1010");
        } else if (c == 'b') {
            str = revese("1011");
        } else if (c == 'c') {
            str = revese("1100");
        } else if (c == 'd') {
            str = revese("1101");
        } else if (c == 'e') {
            str = revese("1110");
        } else if (c == 'f') {
            str = revese("1111");
        } else {
            System.out.print(t);
            return;
        }
        cal(str);
    }

    private static String revese(String str) {
        char[] arr = str.toCharArray();
        String temp = "";
        for (int i = arr.length - 1; i >= 0; i--) {
            temp += arr[i];
        }
        return temp;
    }

    private static void cal(String str) {
        if (str.equals("1010")) {
            System.out.print("A");
        } else if (str.equals("1011")) {
            System.out.print("B");
        } else if (str.equals("1100")) {
            System.out.print("C");
        } else if (str.equals("1101")) {
            System.out.print("D");
        } else if (str.equals("1110")) {
            System.out.print("E");
        } else if (str.equals("1111")) {
            System.out.print("F");
        } else {
            char[] arr = str.toCharArray();
            int sum = 0;
            for (int i = 0; i < arr.length; i++) {
                if ('1' == arr[i]) {
                    int count = arr.length - i - 1;
                    if (count == 0) {
                        sum += 1;
                    } else if (count == 1) {
                        sum += 2;
                    } else {
                        int t = 1;
                        while (count > 0) {
                            t *= 2;
                            count--;
                        }
                        sum += t;
                    }
                }
            }
            System.out.print(sum);
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务