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

字符串合并处理

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

就简单算一下呗


**/****
 * @date:2022.5.14
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] s1 = sc.nextLine().split(" ");
        String s = s1[0] + s1[1];
        int n = 0;
        if(s.length() % 2 == 0)
            n = s.length() / 2;
        else
            n = s.length() /2 + 1;
        char[] ch1 = new char[n];
        int j = 0;
        for (int i = 0; i < s.length(); i += 2) {
            ch1[j++] = s.charAt(i);
        }
        j = 0;
        char[] ch2 = new char[s.length() - n];
        for (int i = 1; i < s.length(); i += 2) {
            ch2[j++] = s.charAt(i);
        }
        Arrays.sort(ch1);
        Arrays.sort(ch2);
        String str = "";
        for (int i = 0; i < ch2.length; i++) {
            str += ch1[i];
            str += ch2[i];
        }
        if(s.length() % 2 != 0)
            str += ch1[n - 1];
        String re = "";
        for (int i = 0; i < str.length(); i++) {
            if(str.charAt(i) == '0')
                re += str.charAt(i);
            else if(str.charAt(i) == '1')
                re += '8';
            else if(str.charAt(i) == '2')
                re += '4';
            else if(str.charAt(i) == '3')
                re += 'C';
            else if(str.charAt(i) == '4')
                re += '2';
            else if(str.charAt(i) == '5')
                re += 'A';
            else if(str.charAt(i) == '6')
                re += '6';
            else if(str.charAt(i) == '7')
                re += 'E';
            else if(str.charAt(i) == '8')
                re += '1';
            else if(str.charAt(i) == '9')
                re += '9';
            else if(str.charAt(i) == 'a' || str.charAt(i) == 'A')
                re += '5';
            else if(str.charAt(i) == 'b' || str.charAt(i) == 'B')
                re += 'D';
            else if(str.charAt(i) == 'c' || str.charAt(i) == 'C')
                re += '3';
            else if(str.charAt(i) == 'd' || str.charAt(i) == 'D')
                re += 'B';
            else if(str.charAt(i) == 'e' || str.charAt(i) == 'E')
                re += '7';
            else if(str.charAt(i) == 'f' || str.charAt(i) == 'F')
                re += 'F';
            else re += str.charAt(i);
        }
        System.out.println(re);
    }
}
 js
全部评论

相关推荐

有工作后先养猫:太好了,是超时空战警,我们有救了😋
点赞 评论 收藏
分享
走不到的路就这样算了吗:大佬硬气
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务