大数求和

 

java基本类型

 

① int的最大值、最小值:

 

max_int = 10位数字(10进制)

              = 2^31 - 1 = 2147483647

 

 

min_int = 10位数字(10进制)

             = -2^31 = -2147483648

 

 

② long的最大值、最小值:

 

max_long = 19位数字(10进制)

                 = 2^63 - 1

 

 

min_long = 19位数字(10进制)= 9223372036854775807 

                = -2^63

 

 

③ double的最大值、最小值

  

max_double = 309位数字(10进制)= 1.7976931348623157E308

                     = (2-2^(-52)) * 2^1023 = 1.7976931348623157E308

 

 

min_double = 324位数字(10进制)

                    = 2^(-1074) = 4.9E-324

 

 

④ float的最大值、最小值:

  

max_float = 39位数字(10进制)= 3.4028234663852886E38

                 = (2-2^(-23)) * 2^127 = 1.7976931348623157E308

 

 

min_float = 45位数字(10进制)= 1.401298464324817E-45

                = 2^(-149) = 1.401298464324817E-45

 

 

java实现大数求和

        将大数按每10位数字分段,求每段求分段和,然后将每段和叠加返回结果;

 

/**
 *  大数相加的返回结果(每10位数字一个结果)
 * 
 * @author mengka.hyy
 *
 */
public class BigNumberDO implements Serializable{

	private static final long serialVersionUID = -7980053973705285378L;
	
	public int count;//第几段

	public int carryBit = 0;//是否需要向前一段数字,进一位
	
	public long subsectionSum;//分段和

	public int getCarryBit() {
		return carryBit;
	}

	public void setCarryBit(int carryBit) {
		this.carryBit = carryBit;
	}

	public long getSubsectionSum() {
		return subsectionSum;
	}

	/**
	 *  subsectionSum只保留最后10位的值,多于10位的会去除
	 * 
	 * @param subsectionSum
	 */
	public void setSubsectionSum(long subsectionSum) {
		String s = String.valueOf(subsectionSum); 
		int index = 0;
		String buffer = new String();
		for(int i=0;i<s.length();i++){
			if(index<10){
				buffer = s.charAt(s.length()-1-i) +buffer;
			}
			index++;
		}
		this.subsectionSum = Long.parseLong(buffer.toString());
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
	
}
import java.util.ArrayList;
import mengka.bigNumber_02.util.BigNumberDO;

public class BigNumberUtil {

	/**
	 * 求2个超出long最大值范围的数字的和
	 * 
	 * @param num1
	 * @param num2
	 * @return
	 */
	public static String sum(String num1, String num2) {
		String sum = "0";
		String[] a1 = splitNum(num1);
		String[] a2 = splitNum(num2);
		sum = sumSubsectionArray(a1, a2);
		return sum;
	}

	public static String sumSubsectionArray(String[] a1, String[] a2) {
		String sum = "0";
		int maxLength = a1.length >= a2.length ? a1.length : a2.length;
		ArrayList<BigNumberDO> list = new ArrayList<BigNumberDO>();
		for (int i = 0; i < maxLength; i++) {
			String subsection1 = i < a1.length ? a1[i] : null;
			String subsection2 = i < a2.length ? a2[i] : null;
			BigNumberDO bigNumberDO = sumSubsection(subsection1, subsection2,
					i + 1);
			list.add(bigNumberDO);
		}
		sum = getSumBigNumber(list);
		return sum;
	}

	public static String getSumBigNumber(ArrayList<BigNumberDO> list) {
		StringBuffer sum = new StringBuffer();
		String[] result = new String[list.size() + 1];// 多申请了一个空间,防止进位溢出
		for (int i = 0; i < list.size(); i++) {
			BigNumberDO bigNumberDO = list.get(i);
			int count = bigNumberDO.getCount() - 1;
			if (bigNumberDO.getCarryBit() > 0) {
				long tmp = result[count + 1] == null ? 0 : Long
						.parseLong(result[count + 1]);
				result[count + 1] = String.valueOf(tmp
						+ bigNumberDO.getCarryBit());
			}
			long tmp = result[count] == null ? 0 : Long
					.parseLong(result[count]);
			result[count] = String
					.valueOf(bigNumberDO.getSubsectionSum() + tmp);
		}
		for (int i = list.size(); i >= 0; i--) {
			if (result[i] != null) {
				sum.append(result[i]);
			}
		}
		return sum.toString();
	}

	/**
	 * 分段求和
	 * 
	 * @param subsection1
	 * @param subsection2
	 * @param count
	 * @return
	 */
	public static BigNumberDO sumSubsection(String subsection1,
			String subsection2, int count) {
		BigNumberDO bigNumberDO = new BigNumberDO();
		bigNumberDO.setCount(count);
		if (subsection1 == null) {
			bigNumberDO.setSubsectionSum(Long.parseLong(subsection2));
			bigNumberDO.setCarryBit(0);// 不需要进位
			return bigNumberDO;
		}
		if (subsection2 == null) {
			bigNumberDO.setSubsectionSum(Long.parseLong(subsection1));
			bigNumberDO.setCarryBit(0);// 不需要进位
			return bigNumberDO;
		}
		long sum = Long.parseLong(subsection1) + Long.parseLong(subsection2);
		if (String.valueOf(sum).length() > 10) {
			bigNumberDO.setCarryBit(1);
		}
		bigNumberDO.setSubsectionSum(sum);
		return bigNumberDO;
	}

	/**
	 * 将一个大数按10位一段切分成一个数组
	 * 
	 * @param num
	 * @return
	 */
	public static String[] splitNum(String num) {
		int size;

		if ((num == null) || ((size = num.length()) == 0)) {
			return null;
		}

		int subsectionCount = size % 10 > 0 ? size / 10 + 1 : size / 10;
		String[] subsections = new String[subsectionCount];

		for (int j = 0; j < subsectionCount; j++) {
			int fromValue = j * 10;
			int toValue = (j == subsectionCount - 1) ? size : j * 10 + 10;
			String buffer = new String();
			for (int i = fromValue; i < toValue; i++) {
				buffer = num.charAt(size - 1 - i) + buffer;
			}
			subsections[j] = buffer.toString();
		}
		return subsections;
	}
}

  

	public static void main(String[] args) {
		
		/**
		 *  例一:
		 *     求2个大数的相加的和
		 */
		String num1 =  "1111111111111111122";//19位
		String num2 = "11111111111111111111";//20位(超过了long的最大值范围)
		String sum = BigNumberUtil.sum(num1, num2);
		System.out.println("sum = "+sum);
		
		
		/**
		 *  例二:
		 *     将一个大数切分成几个求和段
		 */
		String[] aaStrings = BigNumberUtil.splitNum(num1);
		for(int i=0;i<aaStrings.length;i++){
			System.out.println("aaStrings["+i+"] = "+aaStrings[i]);
		}
		
		BigNumberDO bigNumberDO = new BigNumberDO();
		bigNumberDO.setSubsectionSum(45555555551L);
		System.out.println(bigNumberDO.getSubsectionSum());
		
		/**
		 *  例三:
		 *     会进位的大数相加情况
		 */
		String num3 = "1555555555555555555551";
		String num4 =  "156555555555555555666";
		String sum2 = BigNumberUtil.sum(num3, num4);
		System.out.println("sum2 = "+sum2+" , length = "+sum2.length());
		
		String num5 =  "97653278653976532111111";
		String num6 = "765432927123987321111111";
		String sum3 = BigNumberUtil.sum(num5, num6);
		System.out.println("sum3 = "+sum3+" , length = "+sum3.length());
	}

 

 

你可能感兴趣的:(大数求和)