http://sjjg.js.zwu.edu.cn/SFXX/index.html 数据结构
public class Test2 {
/**
* 在二十进制中,我们除了使用数字0-9以外,还使用字母a-j(表示10-19), 给定两个二十进制整数,求它们的和。
* 输入是两个二十进制整数,且都大于0,不超过100位; 输出 展开更多介绍
* 在二十进制中,我们除了使用数字0-9以外,还使用字母a-j(表示10-19), 给定两个二十进制整数,求它们的和。
* 输入是两个二十进制整数,且都大于0,不超过100位; 输出是它们的和(二十进制), 且不包含首0。我们用字符串来表示二十进制整数。
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println(change10To20(789784646, ""));
System.out.println(Test2.cal20Deg("b", "9"));
}
public static String cal20Deg(String num1, String num2) {
int i = 0;
int j = 0;
if (num1.matches("[1-9a-j][0-9a-j]{0,99}")
&& num2.matches("[1-9a-j][0-9a-j]{0,99}")) {
return change10To20(change20To10(num1) + change20To10(num2), "");
} else {
return "输入不合法";
}
}
public static int change20To10(String num) {
int result = 0;
for (int i = 0; i < num.length(); i++) {
int temp = 0;
temp = (int) Math.pow(20, num.length() - 1 - i);
if (num.charAt(i) >= 97 && num.charAt(i) <= 106) {
temp *= num.charAt(i) - 87;
} else {
temp *= Integer.parseInt(String.valueOf(num.charAt(i)));
}
result += temp;
}
return result;
}
public static String change10To20(int num, String temp) {
if (num < 20) {
if (num >= 10) {
return (char) (num + 87) + temp;
} else {
return num + temp;
}
} else {
temp = change10To20(num / 20,
(num % 20 >= 10 ? (char) (num % 20 + 87) : num % 20+"") + temp);
return temp;
}
}
}