华为2017年8月30日校招编程真题2-自定义的26进制和十进制之间互相转换

xiaoxiao2021-02-27  187

题目

二十六进制和十进制之间转换。 使用字符-十进制之间的关系 a-1 b-2 … z-26 aa-27 ab-28 …. huawei-104680767

现在,输入字符串或者整数,要求输出对应的整数或者字符串。


思路


AC(通过70+%,没查出错误)

package test.test2; import org.junit.Test; /** * description: * * @author liyazhou * @since 2017-08-30 19:57 */ public class Main { @Test public void test(){ String[] strs = {"a", "aa", "aaa", "ab", "az", "z", "i", "huawei", "26", "27", "27", "27", "104680767", }; for (String str: strs){ String result = convertTo26(str); System.out.println(str + " :: " + result); } } // public static void main(String[] args){ // Scanner sc = new Scanner(System.in); // String s = sc.next(); // String result = new Main().convertTo26(s); // System.out.println(result); // } public String convertTo26(String str){ char[] chars = str.toCharArray(); String result = ""; if (chars[0] >= '0' && chars[0] <= '9'){ result = numToStr(chars); }else{ result = strToNum(chars) + ""; } return result; } private String numToStr(char[] chars){ StringBuilder result = new StringBuilder(); int num = Integer.valueOf(new String(chars)); while (num != 0){ int re = num % 26; if (re > 0){ char r = (char)(re + ('a' - 1)); result.append(r); } num /= 26; } return result.reverse().toString(); } private int strToNum(char[] chars){ int num = 0; int counter = 0; int base = 1; for (int i = chars.length-1; i >= 0; i --){ base = 1; for (int j = 0; j < counter; j ++){ base *= 26; } num += (chars[i] - 'a' + 1) * base; counter ++; } return num; } }
转载请注明原文地址: https://www.6miu.com/read-13444.html

最新回复(0)