OJ训练题之按照指定规则合并字符串
来源:互联网 发布:矩阵的秩与线性相关 编辑:程序博客网 时间:2024/06/07 14:07
题目如下——
按照指定规则对输入的字符串进行处理。
详细描述:
将输入的两个字符串合并。
对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。这里的下标意思是字符在字符串中的位置。
对排训后的字符串进行操作,如果字符为‘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[n]; 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; } }}
0 0
- OJ训练题之按照指定规则合并字符串
- JS之按照Unicode返回指定字符串
- 按照“规则”拆分字符串
- Jquery 按照指定规则排序
- 华为OJ训练之0041-170115-合并数据表记录
- 按照指定格式拼接字符串
- 按照指定长度切割字符串
- 华为OJ中级题-字符串合并处理
- OJ-----字符串合并处理
- 数据库按照指定的规则排序
- 将指定字符串,按照指定格式分开
- 华为OJ训练之0015-170102-分隔字符串
- 华为OJ训练之0019-170105-字符串匹配
- 华为OJ训练之0023-170107-字符串的相似度
- 华为OJ训练之0035-170112-字符串加解密
- 华为OJ训练之0047-170222-字符串加密
- java按照规则进行字符串的填充
- 华为oj 字符串合并处理
- POJ1562 Oil Deposits(图论入门)
- 3Sum
- 蜗牛—Android基础之RatingBar
- 创建一个Eclipse项目
- 黑马程序员 Java基础知识总结-IO流(应用总结)
- OJ训练题之按照指定规则合并字符串
- c# 注册表编辑
- eclipse提示前景色为黑色Colors
- C++游戏开发需要阅读的书籍 (转)
- window7下用IIS建站点及配置
- hdu1010 Tempter of the Bone
- hdf5 vs cpickle
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- PAT Advanced Level 1077. Kuchiguse (20)