任意实数的精确求幂
北大POJ的题目。。
对一个不大于100的实数(例如,98.2345)求幂,求幂次方不超过25。
我试过用分段存储的方式实现,将实数先全部转为整数按照10000进制存下来,再多次相乘。不过会溢出。还有就的小数点的位置也不太好计算。
求一个算法的思路。
[解决办法]
自己实现一个大数相乘和相加的算法
然后把要求幂的数先转化成整数
记下小数的位数
再先按大数相乘把结果求出来
把小数位置上就OK了
[解决办法]
上代码(java版),没事写着练练手。。。
import java.util.regex.*;public class BigNumber { public String add(String s1,String s2) throws Exception{ if(s1 == null) throw new Exception("输入错误"); if(s2 == null) throw new Exception("输入错误"); String regex = "[\\d]+$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(s1); if(!matcher.find()) throw new Exception("输入错误"); matcher = pattern.matcher(s2); if(!matcher.find()) throw new Exception("输入错误"); String result1 = ""; int minLen =0; int maxLen = 0; String longString = ""; String shortString = ""; if(s1.length() > s2.length()) { maxLen = s1.length(); minLen = s2.length(); longString = s1; shortString = s2; }else { maxLen = s2.length(); minLen = s1.length(); longString = s2; shortString = s1; } int carry = 0; StringBuffer result = new StringBuffer(); int tempResult = 0; for(int i = maxLen-1,j=minLen-1;j>-1;i--,j--) { tempResult = Integer.parseInt(longString.substring(i, i+1)) + Integer.parseInt(shortString.substring(j, j+1)) + carry; result.append(tempResult%10); carry = (int)(tempResult/10); } for(int i=maxLen-minLen-1;i>-1;i--) { tempResult = Integer.parseInt(longString.substring(i, i+1)) + carry; result.append(tempResult%10); carry = (int)(tempResult/10); } if(carry>0) { result.append(carry); } return result.reverse().toString(); } private String multi(String s1,String s2) throws Exception { if(s1 == null) throw new Exception("输入错误"); if(s2 == null) throw new Exception("输入错误"); if(s2.length()!=1) throw new Exception("输入错误"); String regex = "[\\d]+$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(s1); if(!matcher.find()) throw new Exception("输入错误"); matcher = pattern.matcher(s2); if(!matcher.find()) throw new Exception("输入错误"); String result1 = ""; int tempResult = 0; int carry = 0; StringBuffer result = new StringBuffer(); int multi = Integer.parseInt(s2); for(int i=s1.length()-1;i>-1;i--) { tempResult = Integer.parseInt(s1.substring(i, i+1)) * multi + carry; result.append(tempResult%10); carry = (int)(tempResult/10); } if(carry>0) { result.append(carry); } return result.reverse().toString(); } public String multiple(String s1,String s2) throws Exception { if(s1 == null) throw new Exception("输入错误"); if(s2 == null) throw new Exception("输入错误"); String regex = "[\\d]+$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(s1); if(!matcher.find()) throw new Exception("输入错误"); matcher = pattern.matcher(s2); if(!matcher.find()) throw new Exception("输入错误"); String result = "0"; String tempResult = ""; StringBuffer tempBuffer; for(int i=0;i<s2.length();i++) { tempResult = this.multi(s1, s2.substring(i, i+1)); tempBuffer = new StringBuffer(tempResult); for(int j=i;j<s2.length()-1;j++) { tempBuffer.append("0"); } result = this.add(tempBuffer.toString(), result); } return result; } public String power(double i,int j) throws Exception { int temp = (int)(Math.rint(Math.log10(((Integer.parseInt(String.valueOf(i).replace(".", ""))+0.0)/i)))); String tempResult = String.valueOf(i).replace(".", ""); String result = tempResult; for(int k=0;k<j-1;k++) { result = this.multiple(result, tempResult); } result = result.substring(0, result.length()-temp*j)+"."+result.substring(result.length()-temp*j, result.length()); return result; } public static void main(String []args) throws Exception{ BigNumber bigNumber = new BigNumber(); System.out.println(bigNumber.add("9999", "11")); System.out.println(bigNumber.multi("93333", "9")); System.out.println(bigNumber.multiple("12344", "456")); System.out.println(12344*456); System.out.println(bigNumber.power(1.12, 3)); }}
[解决办法]
我的代码,好久之前写的了,具体的你看看吧,希望有所帮住
//1001#include <iostream>int ss[155];using namespace std;unsigned int f1(char * s,unsigned int *s1){ int i, j ; for (i=0,j=0;i<6&&s[i];++i) if (s[i]!='.') ss[j++] = s[i] - '0' ; for (j=0;j<5;++j) s1[j] = ss [ 4 - j ] ; i = 0 ; while ((s[i]!='.')&&s[i]) ++i; j= 5 - i ; return j;}void mul(unsigned int * s1 ,unsigned int * s2){ int i , j , c ; for ( i = 0 ; i < 155 ; ++i ) { ss[ i ] = 0 ; } for ( i = 0 ; i < 5 ; ++i ) { for ( j = 0 ; j < 155 ; ++j ) { ss[ i + j ] += s1[ i ] * s2[ j ] ; } } for ( i = 0 , c = 0 ; i < 155 ; ++i ) { s2[ i ] = ( c + ss[ i ] ) % 10 ; c = ( c + ss[ i ] ) / 10 ; }}int main(){ int i , j , m , k , n ; char s[6] ; unsigned s1[155],s2[155]; while ( scanf( "%s%d",&s,&n) != EOF ) { for ( i = 0 ; i < 155 ; ++i ) { s1[ i ] = 0 ; s2[ i ] = 0 ; ss[ i ] = 0 ; } j = f1( s , s1 ) ; f1( s , s2 ) ; for ( i = 1 ; i < n ; ++ i ) mul( s1 , s2 ) ; i = 154 ; while ( ( !s2[ i ] ) && i ) --i ; k = 0 ; while ( ( ! s2[ k ] ) && ( k < 155 ) ) ++k ; for ( m = i ; m >= n*j ; --m ) printf("%d",s2[m]) ; if (j&&n*j>=k+1) printf("."); for (m = n*j - 1 ; m >= k ; --m ) printf("%d",s2[m]) ; cout<<endl; } //system("pause") ; return 0 ;}