Blue Bridge Cup simple questions: hexadecimal to octal (JAVA version)

First of all, I would like to say sorry to the little friends who are waiting for the program ape's growth path series. The Spring Festival is coming, and it may take a while longer, but the draft is already being written, and the release will be released after the Spring Festival
. A question about the Blue Bridge Cup to practice your hands, hey, I found a question, not much to say, let’s go to the question

topic

insert image description here
The topic is easy to understand, that is, you need to input n numbers and convert them from hexadecimal to octal. The general practice is also as suggested here. It is necessary to convert to decimal first and then to octal, but I think converting to decimal first will waste the computing power of the computer and increase the time complexity of the algorithm. This conversion can be completely optimized.

My optimization algorithm idea

My algorithm is to directly convert hexadecimal to octal. The algorithm has the following requirements:

  1. The input content is a string, so that large number operations can be performed
  2. Only one for loop can be used at most to reduce the time and space complexity of the algorithm.
  3. The returned value is also a string

Let me introduce my algorithm:

  1. Convert the input string to a character array
  2. Starting from the low bit (that is, the highest bit of the array), perform operations:
    1. Because it is converted from hexadecimal to octal, in order to keep the data consistent, the number of each digit in octal can be regarded as the 2 in the same position of the original number Power times, it may be difficult to understand, so let me give you an example, (10) 16 = (16) 10 = (20) 8 Here the first digit of the hexadecimal system (the second digit from the lower digit) is 1, when it comes to octal, it is 2, that is, 1 * 2 to the power of 1, (100) 16 = (256) 10 = (400) 10, here is the first digit in hexadecimal (the third digit from the lower digit) It is 1, and it is 4 in octal, that is, 1 * 2 to the power of 2, and so on.
    2. But it should be noted that the low bit will be like the high bit carry, and the carry number here should be saved. We use the incr parameter to save it, and the low bit to high bit carry should be added when converting.
    3. In addition, the number on the current bit has been converted (multiplied by the power of 2) + the carry is likely to exceed 8. At this time, we need to perform a remainder operation to obtain the final value less than 8, which is the octal bit. number. The carry bit should also continue to be saved to facilitate the next operation.
    4. Follow steps 1-3 to calculate the octal result of each bit from the low bit to the high bit. It should be noted that the first digit may have a carry. At this time, the carry needs to be processed. The processing method is similar to steps 2-3. .

the code

package hexToOct;

public class HexToOct {
    
    
	/**
	 * 十六进制转八进制
	 * 原理:1. 从低位往高位处理,每升一位*2
	 * @param args
	 */
	private static String hexToOct(String hex) {
    
    
		char[] ch = hex.toCharArray(); //step1
		StringBuilder builder = new StringBuilder();
		int i = 0, incr = 0; // incr - 累进数, rest - 余数
		//step2
		for (int j = ch.length-1; j>= 0; j--) {
    
    
			int numb = 0;
			// 将十六进制转10进制
			if (ch[j] >= '0' && ch[j] <= '9') {
    
    
				numb = Integer.valueOf(ch[j] - '0');
			} else if(ch[j] >= 'A' && ch[j] <= 'F') {
    
    
				numb = Integer.valueOf(ch[j] - 'A') + 10;
			} else {
    
    
				System.out.println("请输入十六进制数");
				return null;
			}
			// 处理每一位上的数字
			numb = numb << (i++); // numb = numb * (2^i); i = i + 1;
			numb += incr;
			incr = numb / 8; // 求累进数,便于加入下一次的运算
			numb = numb % 8; // 求当前的余数
			builder.append(numb + "");
		}
		//考虑首位进位的情况
		int rest = 0;
		if (incr != 0) {
    
    
			while(incr > 0) {
    
    
				rest = incr % 8;
				incr = incr / 8;
				builder.append(rest + ""); 
			}
		}
		return builder.reverse().toString();
	}
	
	
	public static void main(String[] args) {
    
    
		System.out.println(hexToOct("8A24B"));
	}
}

operation result

2121113

algorithmic complexity

O(n)

Guess you like

Origin blog.csdn.net/qq_31236027/article/details/122765386