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


题目描述
按照指定规则对输入的字符串进行处理。
详细描述:
将输入的两个字符串合并。
对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。这里的下标意思是字符在字符串中的位置。
对排序后的字符串进行操作,如果字符为‘0’——‘9’或者‘A’——‘F’或者‘a’——‘f’,则对他们所代表的16进制的数进行BIT倒序的操作,并转换为相应的大写字符。如字符为‘4’,为0100b,则翻转后为0010b,也就是2。转换后的字符为‘2’; 如字符为‘7’,为0111b,则翻转后为1110b,也就是e。转换后的字符为大写‘E’。

举例:输入str1为"dec",str2为"fab",合并为“decfab”,分别对“dca”和“efb”进行排序,排序后为“abcedf”,转换后为“5D37BF”
接口设计及说明:
/*
功能:字符串处理
输入:两个字符串,需要异常处理
输出:合并处理后的字符串,具体要求参考文档
返回:无
*/
void ProcessString(char* str1,char *str2,char * strOutput)
{
}


输入描述:
输入两个字符串
输出描述:
输出转化后的结果
示例1
输入

dec fab
输出

5D37BF

  1. import java.util.Scanner;  
  2.   
  3. /** 
  4.  * 题目描述 编写一个程序,将输入字符串中的字符按如下规则排序。 
  5.  */  
  6. public class Main {  
  7.     public static void main(String[] args) {  
  8.         Scanner scanner = new Scanner(System.in);  
  9.         while (scanner.hasNext()) {  
  10.             String s = scanner.next();  
  11.             s += scanner.next();  
  12.             char[] chars = s.toCharArray();  
  13.             String result = exchangeString(chars);  
  14.             System.out.println(result);  
  15.         }  
  16.   
  17.     }  
  18.   
  19.     private static String exchangeString(char[] chars) {  
  20.         int num = chars.length;  
  21.         if (num >= 2) {  
  22.             sortChars(chars, 0);  
  23.   
  24.             sortChars(chars, 1);  
  25.         }  
  26.         String result = "";  
  27.         char temp;  
  28.         char cTemp;  
  29.         for (int i = 0; i < chars.length; i++) {  
  30.             temp = chars[i];  
  31.             if ((temp >= '0' && temp <= '9') || (temp >= 'a' && temp <= 'f') || (temp >= 'A' && temp <= 'F')) {  
  32.                 cTemp = reverse(temp).charAt(0);  
  33.                 if (cTemp >= 'a' && cTemp <= 'z') {  
  34.                     cTemp -= 32;  
  35.                 }  
  36.                 result += cTemp;  
  37.             } else {  
  38.                 result += temp;  
  39.             }  
  40.   
  41.         }  
  42.         return result;  
  43.     }  
  44.   
  45.     private static String reverse(char c) {  
  46.         int bin = Integer.valueOf(c + "", 16);  
  47.         int temp = 1;  
  48.         String string = "";  
  49.         int i = 1;  
  50.         while (i <= 4) {  
  51.             if ((bin & temp) != 0) {  
  52.                 string += '1';  
  53.             } else {  
  54.                 string += '0';  
  55.             }  
  56.             temp = temp << 1;  
  57.             i++;  
  58.         }  
  59.         bin = Integer.valueOf(string, 2);  
  60.         return Integer.toHexString(bin);  
  61.     }  
  62.   
  63.     private static void sortChars(char[] chars, int start) {  
  64.         int num = chars.length;  
  65.         int min = 0;  
  66.         char temp = 0;  
  67.         for (int j = start; j < num; j = j + 2) {  
  68.             min = j;  
  69.             for (int i = j + 2; i < num; i = i + 2) {  
  70.                 if (chars[min] > chars[i]) {  
  71.                     min = i;  
  72.                 }  
  73.             }  
  74.             if (min > j) {  
  75.                 temp = chars[min];  
  76.                 chars[min] = chars[j];  
  77.                 chars[j] = temp;  
  78.             }  
  79.   
  80.         }  
  81.   
  82.     }  
  83.   
  84. }  
原文地址:https://www.cnblogs.com/wwjldm/p/7218497.html