字符串合并处理java华为机试

题目描述

按照指定规则对输入的字符串进行处理。

详细描述:

将输入的两个字符串合并。

对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。这里的下标意思是字符在字符串中的位置。

对排序后的字符串进行操作,如果字符为‘0’——‘9’或者‘A’——‘F’或者‘a’——‘f’,则对他们所代表的16进制的数进行BIT倒序的操作,并转换为相应的大写字符。如字符为‘4’,为0100b,则翻转后为0010b,也就是2。转换后的字符为‘2’; 如字符为‘7’,为0111b,则翻转后为1110b,也就是e。转换后的字符为大写‘E’。

解题思路:主要分为两块,其一是将两字符串合并,再取出奇偶位分别排序,再合并,其二对0-9,a-f,A-F之间的字符进行反转,非这三个区间的字符不变。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            String str1 = sc.next();//空格结束,nextline是enter结尾
            String str2 = sc.next();
            String str3 = str1 + str2;
            char c[] = str3.toCharArray();
            ArrayList<Character> list1 = new ArrayList<>();
            ArrayList<Character> list2 = new ArrayList<>();
            for (int i = 0; i < c.length; i++) {
                if (i % 2 == 0) {
                    list1.add(c[i]);
                } else if (i % 2 == 1) {
                    list2.add(c[i]);
                }
            }
            Collections.sort(list1);
            Collections.sort(list2);
            char[] array1 = new char[str3.length()];
            int j = 0, k = 0;
            for (int i = 0; i < array1.length; i++) {
                if (i % 2 == 0) {
                    array1[i] = list1.get(j++);
                } else {
                    array1[i] = list2.get(k++);
                }
            }
            for (int i = 0; i < array1.length; i++) {
                if ((array1[i] >= '0' && array1[i] <= '9') || (array1[i] >= 'a' && array1[i] <= 'f') || (array1[i] >= 'A' && array1[i] <= 'F')) {
                    System.out.print(fanzhuan(array1[i]));
                } else {
                    System.out.print(array1[i]);
                }
            }
            System.out.println();
        }
    }
    /******************************************/
    public static char fanzhuan(char ch) {
        String s = "";
        if (ch >= '0' && ch <= '9') {
            s = Integer.toBinaryString(ch - '0');
        }
        if (ch >= 'a' && ch <= 'f') {
            s = Integer.toBinaryString(ch - 'a' + 10);
        }
        if (ch >= 'A' && ch <= 'F') {
            s = Integer.toBinaryString(ch - 'A' + 10);
        }
        StringBuffer sb = new StringBuffer();
        for (int i = s.length() - 1; i >= 0; i--) {
            sb.append(s.charAt(i));
        }
        while (sb.length() < 4) {
            sb.append('0');
        }
        int a = Integer.parseInt(sb.toString(), 2);
        if (a >= 0 && a <= 9) {
            return (char) (a + '0');
        } else {
            return (char) (a - 10 + 'A');

        }
    }
}

排坑:

1.题目要求一行输入,空格间隔,我用的是nextline,这是换行输入。

2.没看出来题目要求是持续输入,因此我没有使用sc.hashnext(),但是牛客过不了。

3.合并两个集合的时候,需要考虑一长一短的情况

4.toBinaryString转换2进制时,首位为0的时候是不显示的,使用反转是需要注意补全4位。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值