POJ-1001 Exponentiation

Exponentiation

Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 197361 Accepted: 47168

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

http://poj.org/problem?id=1001

这道题目的意思是

输入

R n
R是浮点数 n则是一个整数

输出

第R的n次方
结果若是整数要去掉末尾的0和.
4.0的2次方16.00 应输出16
若是小数进去掉末尾多余的0
1.10的2次方1.2100 应输出1.21
若是小于1则把.前面的0去掉
0.01的2次方0.0001应输出.0001

解释

x最大是99.999 n最大是25 用一般的求幂的方法回越界
这里我采用大数相乘的方法进行解决
就是先把R输入到字符串中str,把str中的.去掉 进行整数的大数乘法 采用快速幂(n不是很大,也可以不使用)
最后再根据不同的输出进行处理

代码

具体的计算在代码中都有注释

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
//传入两个数字符串的相反串 返回相加结果的相反串 
string Add(string l,string r)
{
	string res="";//储存结果 
	int jinwei=0,tmp,i=0;//jinwei表示进位  tmp表示每轮计算的值 
	while(i<l.size()&&i<r.size())
	{
		tmp=(r[i]-'0')+(l[i]-'0');//计算 
		tmp+=jinwei;//加上上次的进位 
		res+=(tmp%10+'0');//取出个位保留 
		jinwei=tmp/10;//保存进位 
		i++;
	}
	while(i<l.size())//第一个加数有剩余 
	{
		tmp=(l[i]-'0');
		tmp+=jinwei;// 刚进来可能还有进位的 
		res+=(tmp%10+'0');
		jinwei=tmp/10;
		i++;
	}
	while(i<r.size())//第二个加数有剩余
	{
		tmp=(r[i]-'0');
		tmp+=jinwei;
		res+=(tmp%10+'0');
		jinwei=tmp/10;
		i++;
	}
	if(jinwei!=0)  //最后还有进位就保存 
		res+=(jinwei+'0');
	return res;
	
}
//传入两个数字符串 返回相乘的结果串 
string Mutiply(string l,string r)
{
	string res="",now="";//res记录结果的相反字符串 now记录是当前的计算的相反字符串 
	int jinwei=0,tmp;//jinwei 进位 tmp 当前计算的结果 
	int i,j,k;
	for(i=r.size()-1;i>=0;i--)//拿第二个数的每一个数去乘以 
	{
		k=i;
		while(k<r.size()-1)// 乘法有错位 低位补0  字符串是反的 所以在前面补 
		{
			now+='0';
			k++;
		}
		for(j=l.size()-1;j>=0;j--)//依次和第一个乘数进行相乘 
		{
			tmp=(r[i]-'0')*(l[j]-'0');//计算 
			tmp+=jinwei;//加上上次的进位 
			now+=(tmp%10+'0');//取出个位保留 
			jinwei=tmp/10;//保存进位 
		}
		if(jinwei!=0)//最后还有进位就保存 
			now+=(jinwei+'0');
		res=Add(res,now);//进行累相加 
		now="";//清空now 
		jinwei=0;//清空进位 
	}
	reverse(res.begin(),res.end());//将res反转得到答案  
	return res; 
}
string Power(string x, int b)// 快速幂进行计算  这里最大25次方 用不用都可以 
{
    if(b == 0) return "1";
    string t = "1";
    while(b != 0){
    if(b%2) t=Mutiply(t,x);
      x=Mutiply(x,x);
      b/=2;
    }
    return t;
}
int main()
{
	string str;
    int n,dot;//点的位置 
	while(cin >> str >> n)
	{
		int fi=str.find('.');
		if(fi==-1)//是整数  按输入的规则应该不会有整数 
		{
			string s=Power(str,n);
			cout<< s <<endl;
		}
		else
		{
			dot=str.size()-fi-1;//找到小数点后有几位小数 
			str.erase(fi,1);//删除小数点 
			dot=dot*n;//结果的小数点后有几位小数
			int i;
			for(i=0;i<str.size();i++)
			{
				if(str[i]!='0')
				   break;
			}
			if(i>=str.size())//如果都是0 不需要进行计算 
			{
				cout<< '0' <<endl;
				continue;
			}
			string s=Power(str,n);
			s.insert(s.size()-dot,".");//合适的位置插入. 
			int s1=s.size()-1;
			while(s1) 
			{
				if(s[s1]=='0')
				{
					s.erase(s1,1);//去掉后面多余的0 
					s1--;
				}
				else if(s[s1]=='.')//去掉后面多余的. 说明结果是整数 
				{
					s.erase(s1,1);
					s1--;
					break;
				}
				else
				{
					break;
				}
			}
			s1=0;
			while(s[s1]=='0')//去掉开头的0 
			{
				s.erase(s1,1);
			}
			cout<< s <<endl;
		} 
	}
	return 0;
} 

人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

じ☆夏妮国婷☆じ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值