POJ-1001 Exponentiation(高精度n次方)

POJ-1001 Exponentiation(高精度n次方)

      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 R n 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
Hint
If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer
C++

while(cin>>s>>n)

{

...

}

c

while(scanf("%s%d",s,&n)==2) //to  see if the scanf read in as many items as you want

/*while(scanf(%s%d",s,&n)!=EOF) //this also work    */

{

...

}
#include <cstdio>  
#include <iostream>  
#include <string> 
#include <vector>
#include <algorithm>  
using namespace std;  
bool notpoint_standard(string &s)  
{  
    int i; 
    while (!s.empty() && '0' == s[0])//判断都要带上!s.empty 
	 s.erase(s.begin()); //去掉前导0 
    if (s.empty()) s = "0";  //万一就是0组成的,所以补0 ,联系上面消0操作理解
    bool isnot_dot=true;  
    for (i = 0; i < s.size(); i++)  
    {  
        if (s[i]=='.' ) 
		{
	     isnot_dot= false;  
		 break;
	    }
    }  
    if (isnot_dot) 
	return true; 
    while (!s.empty() && '0' == s[s.size()-1]) s.erase(s.end()-1);    //有小数点就要去掉后导0 
    if (!s.empty() && '.' == s[s.size()-1]) s.erase(s.end()-1);      // 去.0000的小数点 ,这3个都是没用的 
    if ( s.empty() ) s = "0";  //去0都要加上这一步保险 
    return false;  
}  
int pointdeal(string &s)  
{  
        int i;
	if (notpoint_standard(s)) 
	return 0;  
    int pointcnt=0;  
    int j = 0;  
    for (i = 0; i < s.size() ; i++)  
    {  
        if (pointcnt> 0) pointcnt++; 
        if (s[i] != '.') s[j++] = s[i];  //去小数点,并找小数点的位置,记录小数点后几位 
        else pointcnt++;   
    }  
    s.erase(s.end()-1);  //因为s[j]比s[i]少了1位 
    return pointcnt-1;  
}  
string multistr(string str1, string str2)  
{  
    int i,j;
    if (str1=="0"||str2=="0") return "0";  
    int a=pointdeal(str1);  
    int b=pointdeal(str2);  
    string ans(str1.size()+str2.size(),'0');  //定义字符串
     for(i=str1.size()-1;i>=0;i--)  
    {  
        int carry=0;  
        int an=str1[i]-'0';  
        for (int j=str2.size()-1;j>=0;j--)  
        {  
            int bn=str2[j]-'0';  
            int sum=an*bn+carry+ans[i+j+1]-'0';   //真正意义的手算乘法
            carry=sum/10;  
            ans[i+j+1]=sum%10+'0';  //最后又要回到字符型
        }  
        if (carry)
		ans[i]+=carry;  
    }  
    if (a>0||b>0)
	ans.insert(ans.end()-a-b,'.');  //每一次都要插回小数点
    notpoint_standard(ans);  
    return ans;  
}  
string pow1(string s, int n)  
{  
     if (s.empty()||s=="0") return "0";//为了程序的健壮性,一定要加上  
     if (n==0) return "1";  
     if (n==1) return s; 
     string  str=pow1(s, n/2);  
     str=multistr(str, str);                          // string^6 6
     if (n % 2) str = multistr(str, s);  //递归算法   //  string^2*string 3
     return str;                                     //   string 1;    以n==6为例
}    
int main()  
{  
    string s;  
    int n;  
    while(cin>>s>>n)  
    {  
	  notpoint_standard(s);
     cout<<pow1(s, n)<<endl;  
    }  
    return 0;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值