题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.next();
String str2 = sc.next();
String str = str1 + str2;
System.out.println(change(sort(str)));
}
}
public static String sort(String str) {
int L = str.length();
List<Character> odd = new ArrayList<>();
List<Character> even = new ArrayList<>();
for (int i = 0; i < L; i++) {
char c = str.charAt(i);
if (i % 2 == 0) even.add(c);
else odd.add(c);
}
Collections.sort(odd);
Collections.sort(even);
String res = "";
for (int i = 0, j = 0, k = 0; i < L; i++) {
if (i % 2 == 0) res += even.get(j++);
else res += odd.get(k++);
}
return res;
}
public static String change(String str) {
int L = str.length();
StringBuffer sb = new StringBuffer();
for (int i = L - 1; i >= 0; i--) {
char c = str.charAt(i);
String s = String.valueOf(c);
if (c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f') {
int tmp = Integer.parseInt(s, 16);
String two = Integer.toBinaryString(tmp);
two = new StringBuffer(two).reverse().toString();
int d = 4 - two.length();
if (d != 0) {
for (int j = 1; j <= d; j++) {
two += "0";
}
}
tmp = Integer.parseInt(two, 2);
sb.append(Integer.toHexString(tmp).toUpperCase());
} else sb.append(s);
}
return sb.reverse().toString();
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.next();
String str2 = sc.next();
String str = str1 + str2;
System.out.println(change(sort(str)));
}
}
public static String sort(String str) {
int L = str.length();
List<Character> odd = new ArrayList<>();
List<Character> even = new ArrayList<>();
for (int i = 0; i < L; i++) {
char c = str.charAt(i);
if (i % 2 == 0) even.add(c);
else odd.add(c);
}
Collections.sort(odd);
Collections.sort(even);
String res = "";
for (int i = 0, j = 0, k = 0; i < L; i++) {
if (i % 2 == 0) res += even.get(j++);
else res += odd.get(k++);
}
return res;
}
public static String change(String str) {
int L = str.length();
StringBuffer sb = new StringBuffer();
for (int i = L - 1; i >= 0; i--) {
char c = str.charAt(i);
String s = String.valueOf(c);
if (c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f') {
int tmp = Integer.parseInt(s, 16);
String two = Integer.toBinaryString(tmp);
two = new StringBuffer(two).reverse().toString();
int d = 4 - two.length();
if (d != 0) {
for (int j = 1; j <= d; j++) {
two += "0";
}
}
tmp = Integer.parseInt(two, 2);
sb.append(Integer.toHexString(tmp).toUpperCase());
} else sb.append(s);
}
return sb.reverse().toString();
}
}