标签:大数
大数结构,tag 中可以放(正负号)
wei可以放长度
num放 一位的数值
next 放下一位(更高的一位)
public class BigNum{
boolean tag = true;
int num = 0;
int wei= 1;
BigNum next;
}
public class shuju {
public static void main(String[] args) {
// TODO Auto-generated method stub
BigNum a = new BigNum();
getDemo(a);
System.out.println("---------a------------------");
out(a);
BigNum b = new BigNum();
getDemo(b);
System.out.println("-----------b---------------");
out(b);
BigNum bigNum = add(a, b);
System.out.println("-----------------------------");
out(bigNum);
}
private static void out(BigNum bigNum) {
for(BigNum t = bigNum.next;t != null;){
System.out.print(t.num);
t = t.next;
}
}
static BigNum add(BigNum a, BigNum b){
BigNum result = new BigNum();
BigNum temp = result;
for(;(a != null || b!= null);){
if(a == null){
add1(temp, 0, b.num);
}
if(b == null){
add1(temp, 0, a.num);
}else{
add1(temp, a.num, b.num);
a= a.next;
b= b.next;
}
if(temp.next == null){
temp.next = new BigNum();
}
temp = temp.next;
}
return result;
}
public static void add1(BigNum a, int n, int m){
if(a.next == null){
a.next = new BigNum();
}
int tem = 0;
tem = n + m + a.num;
a.num = tem %10;
if(tem /10 >0){
add1(a.next, 1, a.next.num);
}
}
面试的时候不一定是你是否理解,也要看面试你的人是否理解你,因为一般的算法都不是随手可以在纸上搞定的,毕竟不能回退不是吗,
标签:大数
原文地址:http://chaojiit.blog.51cto.com/4851948/1606891