Problem16

简介:


1.package com.shui.mu.yao.io.algorithm;  
2./** 
3. *  
4. * @author shuimuqinghua77 @date 2012-12-02 
5. * 
6. */  
7.  
8.public class Problem16 {  
9.    public static void main(String[] args) throws Exception {  
10.  
11.          
12.        byte[] seed=new byte[]{2};/**如果是{1,0,2,4}*/  
13.        seed=getDigit(seed);/**那么转化后得到{4,2,0,1}便于乘法*/  
14.        byte[] result=new byte[]{1};  
15.        for(int i=0;i<1000;i++){  
16.            result = multiply(seed, result);  
17.        }  
18.        int sum=0;  
19.        for(int i=0;i<result.length;i++){  
20.            sum+=result[i];  
21.        }  
22.        System.out.println(sum);  
23.          
24.    }  
25.  
26.    private static byte[] multiply(byte[] multiplicand/**被乘数**/, byte[] multiplier) throws Exception {  
27.        if(multiplicand.length==0||multiplier.length==0)  
28.        {  
29.            throw new Exception("被乘数/乘数为空!!!");  
30.        }  
31.        if(multiplicand.length<multiplier.length){  
32.            byte[] temp=multiplier;  
33.            multiplier=multiplicand;  
34.            multiplicand=temp;  
35.        }  
36.        byte[] result=new byte[multiplicand.length+multiplier.length];  
37.        for(int i=0;i<multiplier.length;i++){  
38.            for(int j=0;j<multiplicand.length;j++){  
39.                result[i+j]+=(byte) (multiplier[i] * multiplicand[j]);  
40.            }  
41.            /**[29]->[9][2]进位**/  
42.            carry(result,1+multiplicand.length,i);  
43.        }  
44.        if(result[result.length-1]==0){  
45.            byte[] resultDest=new byte[result.length-1];  
46.            System.arraycopy(result, 0, resultDest, 0, result.length-1);  
47.            return resultDest;  
48.        }  
49.          
50.        return result;  
51.    }  
52.  
53.    private static void carry(byte[] result,int length,int base) {  
54.        for(int i=base;i<length+base;i++){  
55.            byte decade=(byte) (result[i]/10);  
56.            if(decade>=1){  
57.                result[i+1]+=decade;  
58.                result[i]=(byte) (result[i]%10);  
59.            }  
60.        }  
61.          
62.    }  
63.  
64.    private static byte[] getDigit(byte[] original) {  
65.        byte[] render = new byte[original.length];  
66.        for (int i = 0; i < original.length; i++) {  
67.            render[original.length - 1 - i] = original[i];  
68.        }  
69.        return render;  
70.    }  
71.  
72.}  

 

相关文章
|
Go C++
P1001 A+B Problem
P1001 A+B Problem
115 0
Divisibility Problem
Divisibility Problem
153 0
|
存储 索引
|
数据库管理