HDU-1063 Exponentiation

Problem 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 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


POJ也有同样的一道题目(题目名也相同),不过HDU的数据相当坑,输入的数据中不一定有小数点,所以一开始直接跪了,后来用字符串处理,又被前缀0给坑了(如00.001),这一个前缀0的坑直接让我放弃这道题,因为在POJ提交直接AC,之后突然想起会不会是这么一个坑,试了一下就AC了,成功从坑里爬了出来.....
思路:
分4种情况处理:
1、整数和小数都为0时直接输出0
2、整数为0,则单独计算答案,在输出相应的答案前输出凑齐小数位数的0.
3、整数非0,小数为0,则直接输出答案
4、整数小数都非0,计算对应n次方的小数位数后,先忽略小数点,将整数小数当成整数来求n次方,根据小数位数在中间输出小数点即可




AC代码:
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;


//计算a^n
string get_ans(int a,int n)
{
	int b[150],tmp;
	for(int i=0;i<150;++i)b[i]=0;
	b[0]=1;
	for(int i=0;i<n;++i)
	{
		tmp=0;
		for(int j=0;j<150;++j)
		{
			tmp=(b[j]*a)+tmp;
			b[j]=tmp%10;
			tmp/=10;
		}
	}
	bool flag = false;
	string ans;
	for(int i=149;i>=0;--i)
	{
		if(!flag&&b[i])flag=true;
		if(flag)ans+=('0'+b[i]);
	}
	return ans;
}

int main()
{
	int a,b,c,n,count,tmp;
	char str[6];
	while(cin>>str>>n)
	{
		count=strlen(str)-2;
		b=0;c=0;
		bool flag=false,Flag=false;
		for(int i=0;i<strlen(str);++i)
		{
			if(str[i]=='.')flag=true;
			else if(flag) c=10*c+(str[i]-'0');
			else
			{
				b=10*b+(str[i]-'0');
				if(b>0&&!Flag)Flag=true;
				if(!Flag)count--;
			}
		}
		if(c)
		{
			if(b>9)count--;
			else if(!b)count++;
			while(c%10==0)
			{
				count--;
				c/=10;
			}
		}
		
		tmp=1;
		for(int i=0;i<count;++i)tmp*=10;
		a=b*tmp+c;
		count*=n;//总的小数位数
		if(!b&&!c){cout<<0<<endl;continue;}
		if(b&&!c){cout<<get_ans(b,n)<<endl;continue;}
		if(!b&&c)
		{
			cout<<".";
			string ans = get_ans(c,n);
			tmp=count-ans.length();
			while((tmp--)>0)cout<<0;
			cout<<ans<<endl;
			continue;
		}
		string ans = get_ans(a,n);	
		int len=ans.length();
		tmp=len-count;
		for(int i=0;i<len;++i)
		{
			if(i==tmp)cout<<".";
			printf("%c",ans[i]);
		}
		cout<<endl;			
	}
    return 0;
}



你可能感兴趣的:(HDU-1063 Exponentiation)