[整理]ACM模拟题讲解(1)-高精度

蓝飞 蓝飞 | 时间:2012-02-17, Fri | 3,911 views
编程算法 

Java中提供了byte、short、int和long表示整数,float和double来表示浮点数,每种类型都有一定的表示范围,当超过了这个范围之后就不能处理了。为了提供对非常大的整数和浮点数的处理,Java提供了BigDecimal和BigInteger。下面的代码演示了BigDecimal和BigInteger的基本用法:

BigDecimal data1 = new BigDecimal("23232123456789.123456789");
BigDecimal data2 = new BigDecimal("23423423123456789.123456789");
BigDecimal data3 = data1.add(data2);
System.out.println(data3.toString());
BigInteger iData1 = new BigInteger("123123123456456789789123456789");
BigInteger iData2 = new BigInteger("12312312323232456456789789123456789");
BigInteger iData3 = iData1.add(iData2);
System.out.println(iData3.toString());

如果不使用这些类库如何实现大整数和大浮点数的计算呢?下面通过ACM模拟题介绍。

1、Exponentiation(不想看英文,可以直接跳到后面的中文部分)

Description

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.
This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input

The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

Output

The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don’t print the decimal point if the result is an integer.

Sample Input

95.123 12

0.4321 20

5.1234 15

6.7592 9

98.999 10

1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721

.00000005148554641076956121994511276767154838481760200726351203835429763013462401

43992025569.928573701266488041146654993318703707511666295476720493953024

29448126.764121021618164430206909037173276672

90429072743629540498.107596019456651774561044010001

1.126825030131969720661201

翻译:题目的含义是计算R(0.0 < R < 99.999)的n(0 < n <= 25)次方。

题目解析:

这个题目的关键是如何表示大的整数或者浮点数。针对本题目,计算n次方。可以单独处理小数的位置,如果R中的包含x位小数,则进行运算后的结果应该包括x*n位小数。可以采用数组表示R,然后进行运算即可。下面的方法供参考。

public static String exponentiation(String data,int n){
	// 找小数点
	int index = data.indexOf(".");
	// 去掉小数点
	String temp = data.replace(".","");
	// 用数组表示输入的数据
	byte[] inputData=new byte[temp.length()];
	// 表示计算结果
	byte[] result = new byte[160];
	// 表示进位
	byte pre=0;
	// 初始化
	for(int i=0;i<inputData.length;i++){
		result[i] = Byte.parseByte(temp.substring(temp.length()-i-1,temp.length()-i));
		inputData[i] = result[i];
	}

	// 计算n-1遍
	for(int i=0;i<n-1;i++){
		// 表示上一次的计算结果
		byte[] tempResult = Arrays.copyOf(result,result.length);
		// 对result初始化
		Arrays.fill(result,0,result.length,(byte)0);
		// 用上一次的计算结果与输入的数据相乘
		for(int k=0;k<inputData.length;k++){
			for(int j=0;j<150;j++){
				// 两位相乘
				byte tempMul = (byte)(tempResult[j]*inputData[k]+result[j+k]);
				// 处理个位
				result[j+k]=(byte)(tempMul%10);
				// 处理进位
				pre=(byte)(tempMul/10);
				// 表示进到第几位
				int pp=1;
				// 处理进位
				while(pre>0){
					tempMul=(byte)(pre+result[j+k+pp]);
					result[j+k+pp]=(byte)(tempMul%10);
					pre=(byte)(tempMul/10);
					pp++;
				}
			}
		}
	}

	// 计算小数位
	int digits;
	if(index==-1)
		digits = 0 ;
	else
		digits = n*(data.substring(index+1).length());

	StringBuffer stringResult = new StringBuffer();
	if(index==-1){ // 没有小数点
		boolean b=false;
		for(int i=result.length-1;i>=0;i--){
			if(!b){ //前面都是0
				if(result[i]==0)
					continue;
				else
					b=true;
			}
			stringResult.append(result[i]);
		}
	}else{ // 有小数点
		boolean b=false;
		// 处理小数点之前
		for(int i=result.length-1;i>=digits;i--){
			if(!b){ //前面都是0
				if(result[i]==0)
					continue;
				else
					b=true;
			}
			stringResult.append(result[i]);
		}
		stringResult.append(".");
		for(int i=digits-1;i>=0;i--){
			stringResult.append(result[i]);
		}
	}
	return stringResult.toString();
}

2、A + B Problem

Problem Description

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input

2

1 2

112233445566778899 998877665544332211

Sample Output

Case 1:

1 + 2 = 3

Case 2:

112233445566778899 + 998877665544332211 = 1111111111111111110

翻译:题目要求就算两个大的整数的和。

题目解析:两个整数分别使用数组表示,然后每一位相加,并对进位处理即可。上一题的内容已经包含这个过程。这里不再给出参考代码。

 

如需转载请注明出处:蓝飞技术部落格

当前暂无评论 »