PKU_1001_Exponentiation

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


Source Code

/**************************
   对乘号进行重载,使其可以进行高精度浮点数的乘法
***************************/
string operator* (string s1,string s2)
{
	int l1=s1.length(),l2=s2.length();
	int i,j,k=0,pos1=0,pos2=0;
	string result="";

	int a[200],b[200],c[200];
	for (i=0;i<200;i++)
	{
		a[i]=0;
		b[i]=0;
		c[i]=0;
	}
	
	//把string类转化为数组类,去掉小数点,并记录小数点位置
	for (i=0;i<l1;i++)
	{	
		if (s1[l1-1-i]=='.')
		{
			k=1;
			pos1=i;
		}
		else
		{
			a[i-k]=s1[l1-1-i]-'0';
		}
	}
	k=0;
	for (i=0;i<l2;i++)
	{	
		if (s2[l2-1-i]=='.')
		{
			k=1;
			pos2=i;
		}
		else
		{
			b[i-k]=s2[l2-1-i]-'0';
		}
	}
	
	//计算结果,并存储于数组C中。
       for (i=0;i<l1-(pos1!=0);i++)
		for (j=0;j<l2-(pos2!=0);j++)
		{
			c[i+j]+=a[i]*b[j];
		}

	for (i=0;i<=l1+l2-2;i++)
	{
		c[i+1]+=c[i]/10;
		c[i]=c[i]%10;
	}
	
	//把数组类重新转化为string类
	k=(pos1!=0)+(pos2!=0);     //判断原string类是否有小数点
	int l=l1+l2-k;             //根据小数点的个数,确定最终答案的位数
	for (i=0;i<l;i++)
	{
		result+=c[l-1-i]+'0';
	}


        //添加小数点
	int pos=l-pos1-pos2;
	if (pos<l)
		result.insert(pos,1,'.');
	return result;
}

/**************************
   采用二分法进行幂的计算
***************************/
string power(string s,int n)
{
	if (n==1) 
		return s;
	else 
	{
		string subResult= power(s,n/2);	
		subResult=subResult * subResult;
		if (n%2==1)
		{
			subResult = subResult * s;
		}
		return subResult;
	}
}


/**************************
   去掉多余的零并进行输出
***************************/
void outPrint(string s)
{
	int end=0,beg=s.length()-1;
	while (s[beg]=='0'||s[beg]==0) beg--;
	if (s[beg]=='.') beg--;
	while (s[end]=='0'||s[end]==0) end++;
	int i;
	for (i=end;i<=beg;i++)
		cout<<s[i];
	cout<<endl;
}

int main()
{
	string s;
	int n;
	while (cin>>s>>n)
	{
		string result=power(s,n);
		outPrint(result);
	}
	return 0;
}

参考链接: http://blog.csdn.net/fanxing1/article/details/5730978
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值