首页 > 代码库 > 计算99的99次方

计算99的99次方

package BigNum;

public class BigNumAdd {
    private String num1st;        // 被加数
    private String num2nd;        // 加数

    public BigNumAdd() {

    }

    public BigNumAdd(String num1st, String num2nd) {
        this.num1st = num1st;
        this.num2nd = num2nd;
    }

    public String add() {
        StringBuffer sum = new StringBuffer();
        int i = 0, a = 0, b = 0;
        int tag = 0;    // 进位数
        int j, k;        // 被加数和加数的位置
        j = num1st.length() - 1;    //模拟加法法则
        k = num2nd.length() - 1;
        while (j >= 0 || k >= 0 || tag > 0) {
            if (j >= 0) {
                a = num1st.charAt(j) - ‘0‘;
            } else
                a = 0;
            if (k >= 0) {
                b = num2nd.charAt(k) - ‘0‘;
            } else
                b = 0;
            try {
                if (a < 0 || a > 9 || b < 0 || b > 9) {
                    throw new NumberFormatException();
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return "Argument Error!";
            }
            i = a + b + tag;
            tag = i / 10;
            j--;
            k--;
            sum = new StringBuffer(String.valueOf(i % 10)).append(sum);
        }
        return sum.toString();
    }
}

大数乘法

package BigNum;

public class BigNumTimes {
    private String num1st;        //被乘数
    private String num2nd;        //乘数
    
    public BigNumTimes() {
        
    }
    
    public BigNumTimes(String num1st, String num2nd) {
        this.num1st = num1st;
        this.num2nd = num2nd;
    }
    
    public String times() {
        int i=0,a=0,b=0;
        int j,k;        //位置标记
        int tag=0;        //进位数
        StringBuffer sum=new StringBuffer("");
        StringBuffer preSum=new StringBuffer("0");
        StringBuffer curSum=new StringBuffer("");        
        k=num2nd.length()-1;    //模拟乘法法则
        while (k>=0) {
            for (int l=0;l<num2nd.length()-1-k;l++){
                curSum.append("0");
            }
            b=num2nd.charAt(k)-‘0‘;
            j=num1st.length()-1;
            while (j>=0 || tag>0){
                if (j >= 0) {
                    a = num1st.charAt(j) - ‘0‘;
                } else
                    a = 0;
                try {
                    if (a<0 || a>9 || b<0 || b>9) {
                        throw new NumberFormatException();
                    }
                    }catch (NumberFormatException e){
                        e.printStackTrace();
                        return "Argument Error!";
                    }
                i=b*a+tag;
                tag=i/10;
                curSum=new StringBuffer(String.valueOf(i%10)).append(curSum);
                j--;
            }
            tag=0;
            k--;
            sum=new StringBuffer(new BigNumAdd(curSum.toString(),preSum.toString()).add());  //一轮结果相加
            preSum=new StringBuffer(sum);
            curSum=new StringBuffer("");
        }
        return sum.toString();
    }
    //99的99次方
    public static void main(String[] args){
        String sum="1";
            for (int i=1;i<=99;i++){
                BigNumTimes bnt=new BigNumTimes(sum,"99");
                sum=bnt.times();
                System.out.println("99的"+i+"次方="+sum);
            }
            //System.out.println("位数="+sum.length());
    }
}

link:http://blog.csdn.net/asdflixiaok/article/details/40486341

计算99的99次方