大数相加、大数相乘

我们首先看大数相加,其基本思路是:

       过程和我们直接笔算思路一样,比如  129 + 23 ,先计算 9+3 = 12 ,则确定个位为2,同时向前进 1 ;继续 2 + 2 + 1 = 5,确定十位为5,同时向前进 0;继续 1 + 0 + 0 = 1,确定百位为1(其中该式中前一个0为12补齐的0,后一个0为进位的0);结束 得到结果为152。代码如下:

package pack1;

public class Multiply {

	/**
	 * @param 大数相加
	 */
	public String add(String str1,String str2){
		int jinwei = 0;
		StringBuffer sb1 = new StringBuffer(str1); sb1.reverse();
		StringBuffer sb2 = new StringBuffer(str2); sb2.reverse();
		int len1=sb1.length(),len2=sb2.length(),minLen = (len1>len2)?len2:len1;
		
		StringBuffer sb = new StringBuffer("");
		for(int i=0;i<minLen;i++){
			int single =Integer.parseInt(sb1.charAt(i)+"")+Integer.parseInt(sb2.charAt(i)+"")+jinwei;
			sb.append(single%10);
			jinwei = single /10;
		}
		
		if(len1==len2){
			if(jinwei!=0)  sb.append(jinwei);
			return sb.reverse().toString();
		
		}else if(len1>len2){
			
			for(int i=len2;i<len1;i++){
				int single =Integer.parseInt(sb1.charAt(i)+"")+jinwei;
				sb.append(single%10);
				jinwei = single /10;
			}
			if(jinwei!=0) sb.append(jinwei);
			return sb.reverse().toString();
		
		}else{
			
			for(int i=len1;i<len2;i++){
				int single =Integer.parseInt(sb2.charAt(i)+"")+jinwei;
				sb.append(single%10);
				jinwei = single /10;
			}
			if(jinwei!=0) sb.append(jinwei);
			return sb.reverse().toString();
		}
	}
	
	
        public static void main(String[] args) {
		Multiply mul = new Multiply();
		System.out.println(mul.add("8882488", "123888234248888"));
		//使用BigInteger
               java.math.BigInteger  big1 = new java.math.BigInteger("8882488");
               java.math.BigInteger  big2 = new java.math.BigInteger("123888234248888");
               System.out.println(big1.add(big2).toString());
	}

}

大数相乘

大数相乘怎么做呢,比如计算129 * 23,算法思路为:将乘法变换为加法进行计算。首先取23的个位3,代表129需要相加3次;然后再取23的十位2,代表129需要相加2次,并且同时需要在乘以10,则可以得到最后的结果。代码如下:

package pack1;

public class Multiply {

	/**
	 * @param 大数相加
	 */
	public String add(String str1,String str2){
		int jinwei = 0;
		StringBuffer sb1 = new StringBuffer(str1); sb1.reverse();
		StringBuffer sb2 = new StringBuffer(str2); sb2.reverse();
		int len1=sb1.length(),len2=sb2.length(),minLen = (len1>len2)?len2:len1;
		
		StringBuffer sb = new StringBuffer("");
		for(int i=0;i<minLen;i++){
			int single =Integer.parseInt(sb1.charAt(i)+"")+Integer.parseInt(sb2.charAt(i)+"")+jinwei;
			sb.append(single%10);
			jinwei = single /10;
		}
		
		if(len1==len2){
			if(jinwei!=0)  sb.append(jinwei);
			return sb.reverse().toString();
		
		}else if(len1>len2){
			
			for(int i=len2;i<len1;i++){
				int single =Integer.parseInt(sb1.charAt(i)+"")+jinwei;
				sb.append(single%10);
				jinwei = single /10;
			}
			if(jinwei!=0) sb.append(jinwei);
			return sb.reverse().toString();
		
		}else{
			
			for(int i=len1;i<len2;i++){
				int single =Integer.parseInt(sb2.charAt(i)+"")+jinwei;
				sb.append(single%10);
				jinwei = single /10;
			}
			if(jinwei!=0) sb.append(jinwei);
			return sb.reverse().toString();
		}
	}
	
    public String multiply(String str1,String str2){
    	int len1 = str1.length();
    	int len2 = str2.length();
    	String result = "0";
    	if(len1<len2){              //使得str2指向的字符串为较短的一个字符串
    		String  temp = str1;
    		str1 = str2;
    		str2 = temp;
    		len1 = str1.length();
        	len2 = str2.length();
    	}  
        
    	for(int i=1;i<=len2;i++){
    		String temp = "0";
    		int num = str2.charAt(len2-i) - '0';
    		for(int j=1;j<=num;j++)
    			temp = add(temp,str1);
            
    		for(int k=1;k<=i-1;k++)
    			temp = temp + "0";
    		
    		result = add(result,temp);
    	}
    	
    	return result;
    }
	
	public static void main(String[] args) {
		Multiply mul = new Multiply();
		System.out.println(mul.multiply("8882488", "123888234248888"));
		//使用BigInteger
        java.math.BigInteger  big1 = new java.math.BigInteger("8882488");
        java.math.BigInteger  big2 = new java.math.BigInteger("123888234248888");
        System.out.println(big1.multiply(big2).toString());
	}

}


你可能感兴趣的:(大数相加、大数相乘)