题解 | #字符串合并处理#
字符串合并处理
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);
String str_1 = in.next();
String str_2 = in.next();
Map<Character, Character> tr = new HashMap<Character, Character>();
tr.put('0', '0');
tr.put('1', '8');
tr.put('2', '4');
tr.put('3', 'C');
tr.put('4', '2');
tr.put('5', 'A');
tr.put('6', '6');
tr.put('7', 'E');
tr.put('8', '1');
tr.put('9', '9');
tr.put('a', '5');
tr.put('b', 'D');
tr.put('c', '3');
tr.put('d', 'B');
tr.put('e', '7');
tr.put('f', 'F');
tr.put('A', '5');
tr.put('B', 'D');
tr.put('C', '3');
tr.put('D', 'B');
tr.put('E', '7');
tr.put('F', 'F');
//第一步
String str = str_1 + str_2;
char[] cs_str = str.toCharArray();
//第二步
char[] cs_str_1 = new char[str.length() / 2 + str.length() % 2];
char[] cs_str_2 = new char[str.length() / 2];
int index_1 = 0;
int index_2 = 0;
for (int i = 0; i < str.length(); i++) {
if (i % 2 == 0) {
cs_str_1[i / 2] = cs_str[i];
} else {
cs_str_2[(i - 1) / 2] = cs_str[i];
}
}
Arrays.sort(cs_str_1);
Arrays.sort(cs_str_2);
int index_1_1 = 0;
int index_1_2 = 0;
for (int i = 0; i < str.length(); i++) {
if (i % 2 == 0) {
cs_str[i] = cs_str_1[i / 2];
} else {
cs_str[i] = cs_str_2[(i - 1) / 2];
}
}
//第三步
for (int i = 0; i < str.length(); i++) {
char c = cs_str[i];
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'F') ||
(c >= 'a' && c <= 'f')) {
cs_str[i] = tr.get(c);;
}
}
System.out.println(new String(cs_str));
}
}
