OJ训练题之按照指定规则合并字符串
2014-05-19 19:40
323 查看
题目如下——
我的程序——
按照指定规则对输入的字符串进行处理。 详细描述: 将输入的两个字符串合并。 对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。这里的下标意思是字符在字符串中的位置。 对排训后的字符串进行操作,如果字符为‘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” |
import java.io.IOException; import java.util.Scanner; public class Main { private static final byte[] small = {0x0A,0x0B,0x0c,0x0d,0x0e,0x0f}; private static final char[] zimu = {'a','b','c','d','e','f'}; public static void main(String[] args) throws IOException { Scanner sf = new Scanner(System.in); // 输入字符串 String[] ssStrings = sf.nextLine().split(" "); String s1 = ssStrings[0]; String s2 = ssStrings[1]; StringBuffer ss1 = new StringBuffer(); StringBuffer ss2 = new StringBuffer(); ss1.append(s1); ss1.append(s2); String s3 = ss1.toString(); ss1.setLength(0); for (int i = 0; i < s3.length(); i++) { if (i % 2 == 1) { ss1.append(s3.charAt(i)); // 奇数 } else { ss2.append(s3.charAt(i)); } } s1 = ss1.toString(); s2 = ss2.toString(); s1 = quickSort(s1); s2 = quickSort(s2); ss1.setLength(0); int length = s1.length(); for (int i = 0; i < length; i++) { ss1.append(s2.charAt(i)); ss1.append(s1.charAt(i)); } s3 = ss1.toString(); System.out.println(convert(s3)); } private static String convert(String string){ int length = string.length(); byte[] ca = new byte[length]; StringBuffer sBuffer = new StringBuffer(); byte temp = 0x01; byte result = 0; //位倒序转换后的结果 char c = 0; for (int i = 0; i < length; i++) { temp =(byte)string.charAt(i); if(temp>= 0x30 && temp<= 0x39){ temp -= 0x30; }else if(temp>= 0x41 && temp<= 0x46){ //大写 temp -= 0x41; temp = small[temp]; }else if(temp>= 0x61 && temp<= 0x66){ //小写 temp -= 0x61; temp = small[temp]; } ca[i] = temp; // System.out.println("倒序前"+ca[i]); temp = 0x01; for (int j = 3; j >-1; j--) { if((ca[i]&temp) == temp){ result += (byte)Math.pow(2, j); } temp <<= 1; } // System.out.println("倒序后"+result); temp = 0x01; if (result>9 && result<16) { c = zimu[result-10]; sBuffer.append(c); }else{ sBuffer.append(result); } result = 0; } return sBuffer.toString().toUpperCase(); } // 对String进行快速排序,升序 public static String quickSort(String string) { int n = string.length(); int[] list = new int ; for (int i = 0; i < list.length; i++) { list[i] = string.charAt(i); } quickSort(list, 0, list.length - 1); StringBuffer sbBuffer = new StringBuffer(); for (int i = 0; i < list.length; i++) { char temp = (char) list[i]; sbBuffer.append(String.valueOf(temp)); } return sbBuffer.toString(); } private static void quickSort(int[] list, int first, int last) { if (last > first) { int pivotIndex = partition(list, first, last); quickSort(list, first, pivotIndex - 1); quickSort(list, pivotIndex + 1, last); } } private static int partition(int[] list, int first, int last) { // 选择第一个为主元素,分开两半 int pivot = list[first]; int low = first + 1; int high = last; while (high > low) { // 从左边往前排序 while (low <= high && list[low] <= pivot) { low++; } // 从右边从往后排序 while (low <= high && list[high] > pivot) { high--; } // 交换两个元素 if (high > low) { int temp = list[high]; list[high] = list[low]; list[low] = temp; } } while (high > first && list[high] >= pivot) { high--; } if (pivot > list[high]) { list[first] = list[high]; list[high] = pivot; return high; } else { return first; } } }
相关文章推荐
- 第十五周oj训练——将字符串插入到另一个字符串的指定位置(串)(2274)
- (华为)按照指定规则对输入的字符串进行处理
- SQL分割字符串,SQL按照指定字符分割字符串,SQL处理字符串...
- 【原创】C#.Net中对中英文混排的字符串按照指定宽度截取排版
- 按照指定长度分割一个字符串
- 华为OJ_2126_字符串合并处理
- 按照指定的格式格式化时间字符串
- 文本字符串按照指定宽度折行显示
- JavaScript fontsize方法入门实例(按照指定的尺寸来显示字符串)
- js 选择checkbox获得值后按照规则拼接字符串,放在数组
- [SQL]实现按照指定分割分分割字符串
- 原创:用VBA实现将鼠标选择的单元格按照指定格式合并并复制到剪切板
- 将指定字符串,按照指定格式分开
- 将字符串按照指定方式就地翻转
- 函数备份:按照指定的分隔符,将字符串进行切分
- C#下按照指定字符进行字符串分割
- SQL分割字符串,SQL按照指定字符分割字符串,SQL处理字符串...
- 将浮点数按照指定位数精度转换成字符串的C++函数
- 将字符串按照指定的长度进行分割(一个中文视为两个字符)
- 替换一个规则字符串中指定的几个字符串