ACM_PKU_1001

Exponentiation
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: * Accepted: *

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

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 */
{
...
}

Source

 
 
#include <iostream>
using namespace std;

inline void IntToB(char* B, int n)
{
	while (n)
	{
		*B = 48 + n % 2;
		B++;
		n /= 2;
	};
	*B = '2';
};

inline void P_100101(char* x1, char* x2, char* re)
{
	char* re_cur1;
	char* re_cur2;
	char* re_p = re;
	char* b_x1;
	b_x1 = x1;
	int tmp;
	int tmp_2;
	int tmp_1;
	while((*x2) != '\0')
	{
		if (*x2 == '0')
		{
			x1 = b_x1;
			x2++;
			re_p++;
			continue;
		};
		re_cur1 = re_cur2 = re_p;
		while((*x1) != '\0')
		{
			if (*x1 == '0')
			{
				x1++;
				re_cur1++;
				continue;
			};
			re_cur2 = re_cur1;
			tmp = ((*x1) - 48) * ((*x2) - 48);
			tmp_1 = tmp / 10;
			tmp_2 = tmp % 10;
			*re_cur2 += tmp_2;
			*(re_cur2 + 1) += tmp_1;
			while (*re_cur2 > 57)
			{
				while (*re_cur2 > 57)
				{
					(*re_cur2) -= 10;
					(*(re_cur2 + 1))++;
				};
				re_cur2++;
			};
			re_cur2++;
			while (*re_cur2 > 57)
			{
				while (*re_cur2 > 57)
				{
					(*re_cur2) -= 10;
					(*(re_cur2 + 1))++;
				};
				re_cur2++;
			};
			x1++;
			re_cur1++;
		};
		x1 = b_x1;
		x2++;
		re_p++;
	};
};

int main()
{
	char result[151];
	char tmp[151];
	char input_ch[6];
	char input[7];
	char B_ary[6];
	int p_in;
	int p_re;
	int p_B;
	B_ary[5] = '2';
	int point;				//point location in length
	int n;
	int i = 0;
	result[150] = tmp[150] = '\0';
	int point_re;
	while(cin>>input>>n)
	{
		p_in = 0;
		p_re = 0;
		p_B = 0;
		input[6] = '.';
		point = 0;
		IntToB(B_ary, n);
		while(input[point++] != '.');
		if (point == 7)
		{
			point = -1;
			point_re = 0;
		};
		p_in = 5;
		while (p_in >= 0)
		{
			if (input[p_in--] != '.') result[p_re] = input_ch[p_re++] = input[p_in + 1];
		};
		input_ch[p_re] = '\0';
		while (p_re < 150) result[p_re++] = '0';
		//
		while (B_ary[p_B++] != '2');
		p_B -= 3;
		while (p_B >= 0)
		{
			//
			for (i = 0; i < 150; i++) tmp[i] = '0';
			P_100101(result, result, tmp);
			p_re = 0;
			while (p_re < 150) result[p_re] = tmp[p_re++];
			if (B_ary[p_B] == '1')
			{
				for (i = 0; i < 150; i++) tmp[i] = '0';
				P_100101(result, input_ch, tmp);
				p_re = 0;
				while (p_re < 150) result[p_re] = tmp[p_re++];
			};
			//
			p_B--;
		};
		//
		if (point != -1)
		{
			point_re = (6 - point) * n;
			i = 0;
			while ((result[i] == '0') && (i < point_re)) result[i++] = '*';
			i = 5 * n;
		}
		else i = 6 * n;
		while ((result[i--] == '0') && (i >= point_re));
		i++;
		if ((i == point_re) && (result[i] == '0'))
		{
			i--;
			if (i >= 0)
			{
				cout<<".";
				while ((i >= 0) && (result[i] != '*'))
				{
					cout<<result[i--];
				};
				cout<<"\n";
			}
			else
				cout<<"\n";
		}
		else
		{
			while (i >= point_re) cout<<result[i--];
			if (i >= 0)
			{
				if (result[i] != '*')
					cout<<'.';
				while ((i >= 0) && (result[i] != '*'))
				{
					cout<<result[i--];
				};
				cout<<"\n";
			}
			else
				cout<<"\n";
		};
	};
	return 1;
};
老天......这么长,不过AC了~~
去网上搜了一下,改了几个地方,终于AC了,留下来读读。
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
class fs{
public:
	void set(char *s);
	fs(){};
	fs mul(fs b);
	void output();
	int prec;
	int len;
	int s[1002];
};
void fs::set(char *t)
{
	int i,j,l,p;
	l=strlen(t);
	for(p=0;p<l;p++)if(t[p]=='.')break;
	if(p==0)prec=0;
	else prec=l-p-1;
	for(i=l-1,j=0;i>=0;i--)if(t[i]!='.')s[++j]=t[i]-'0';
	while(j>1&&s[j]==0)--j;
	len=j;
	return ;
}
fs fs::mul(fs b)
{
	fs c;
	c.prec=prec+b.prec;
	int i,j;
	c.len=len+b.len;
	for(i=1;i<=c.len;i++)c.s[i]=0;
	for(i=1;i<=len;i++)for(j=1;j<=b.len;j++)c.s[i+j-1]+=s[i]*b.s[j];
	for(i=1;i<c.len;i++){c.s[i+1]+=c.s[i]/10;c.s[i]%=10;}
	while(c.s[i]){c.s[i+1]=c.s[i]/10;c.s[i]%=10;i++;}
	while(i>1&&!c.s[i])i--;
	c.len=i;
	return c;
}
void fs::output()
{
	int i,k,j,low=1;
	for(i=len;i>prec;i--)cout<<s[i];
	k=i;
	while(k>0&&s[k]==0)k--;
	if(k==0){
//		cout<<".0"<<endl;
		return ;}
	cout<<".";
	if(len<prec)for(j=0;j<prec-len;j++)cout<<"0";
	while(s[low]==0)low++;
	while(i>=low)cout<<s[i--];
	return ;
}
int main()
{
	int i,n;
	char s[10];
	fs ans,temp;
//	freopen("in.tx","r",stdin);
	while(cin>>s>>n)
	{
		//if(strcmp(s,"
		ans.set(s);
		temp.set(s);
		for(i=0;i<n-1;i++)
		{
			ans=ans.mul(temp);
		}
		ans.output();
		cout<<endl;
	}
	return 0;
};
貌似Java有内置的,搜了一下,没验证,先记下了。
import java.io.*;
import java.math.*;
import java.util.Scanner ; 

public class Main 
{
 public static void main(String [] args )
 {
  Scanner sc = new Scanner(System.in) ; 
  int n ; 
  BigDecimal b ; 
  while (sc.hasNext())
  {
   b = sc.nextBigDecimal() ; 
   n = sc.nextInt() ; 
   b = b.pow(n).stripTrailingZeros() ; 
   String s = b.toPlainString() ; 
   if (s.charAt(0) == '0') 
    System.out.println(s.substring(1)) ;
   else 
    System.out.println(s) ; 
  }
 }
}
为什么Java的这么短~~~ = =!
不过效率的确比不上C++~

转载于:https://my.oschina.net/w0lf/blog/3851

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值