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


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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章