您的位置:首页 > 其它

poj 1001 Exponentiation 高精度乘方

2014-07-20 15:47 375 查看
点击打开链接题目链接

Exponentiation

Time Limit: 500MSMemory Limit: 10000K
Total Submissions: 133711Accepted: 32685
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

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

{

...

}


输入a,n 求a的n次

去掉前置后置零之后 结果的小数点后的位数=n*(a小数点后位数)

贴代码

<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
int main()
{
	int a[10],b[200],dig[10][200];//b[200]记录结果
	char s[10];
	int n,i,j,k,l,m,t;
	int flag;
	int point;
	int count;
	while(scanf("%s %d",s,&n)!=EOF)
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		flag=0;
		point=0;
		for(i=0;i<6;i++)
		{
			if(s[i]=='.')
			{
				flag=1;
				break;
			}
		}
		while(s[0]=='0')
		{
			for(i=0;s[i]!='\0';i++)
				s[i]=s[i+1];
		}
		if(flag==1)
		{
			while(s[strlen(s)-1]=='0')
			{
				s[strlen(s)-1]='\0';
			}
			if(s[strlen(s)-1]=='.')
				s[strlen(s)-1]='\0';
			else
			{
				for(i=0;i<strlen(s);i++)
				{
					if(s[i]=='.')
						point=strlen(s)-i-1;
				}
				for(i=strlen(s)-point;i<=strlen(s);i++)
				{
					s[i-1]=s[i];
				}
			}
		}
		while(s[0]=='0')
		{
			for(i=0;s[i]!='\0';i++)
				s[i]=s[i+1];
		}
		if(strlen(s)==0)
        {
        	printf("0\n");
        	continue;
        }
        l=strlen(s);
        for(i=0;i<l;i++)
        {
        	a[i]=s[i]-'0';
        	b[i]=s[i]-'0';
        }
        point=point*n;
        count=l;
     //   printf("%s \n",s);
        for(i=1;i<n;i++)
        {
        	memset(dig,0,sizeof(dig));
        	for(j=0;j<l;j++)
        	{
        		for(k=0;k<count;k++)
        		{
        			dig[j][count-1-k]=b[count-1-k]*a[l-1-j];
        		}
        		for(k=count-1;k>0;k--)
        		{
        			if(dig[j][k]>=10)
        			{
        				dig[j][k-1]+=dig[j][k]/10;
        				dig[j][k]=dig[j][k]%10;
        			}
        		}
        	}
        	count=count+l-1;
        	memset(b,0,sizeof(b));
        	for(j=count-1;j>=0;j--)
        	{
        		for(k=0;k<l;k++)
        		{
        			if((j+k-l+1)>=0)
        			b[j]+=dig[k][j+k-l+1];
        		}
        	}
        	for(j=count-1;j>0;j--)
        	{
        		if(b[j]>=10)
        		{
        			b[j-1]+=b[j]/10;
        			b[j]=b[j]%10;
        		}
        	}
        	if(b[0]>=10)
        	{
        		count++;
        		for(j=count;j>1;j--)
        		{
        			b[j]=b[j-1];
        		}
        		b[1]=b[0]%10;
        		b[0]=b[0]/10;
        	}
        }
  //      printf("%d %d\n",count,point);
        if(count<point)
        {
        	printf(".");
        	for(i=0;i<point-count;i++)
        		printf("0");
        	for(i=0;i<count;i++)
        		printf("%d",b[i]);
        	printf("\n");
        }
        else
        {
        	if(point==0)
            {
        		for(i=0;i<count;i++)
        			printf("%d",b[i]);
        		printf("\n");
            }
        	else
        	{
        		for(i=0;i<count-point;i++)
        			printf("%d",b[i]);
        		printf(".");
        		for(i=count-point;i<count;i++)
        			printf("%d",b[i]);
        		printf("\n");
        	}
        }
   	}
	return 0;
}


wa了好多次

网上大牛给的特殊数据


<pre name="code" class="cpp">95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12.00001  1
.12345 1
0001.1 1
1.1000 1
10.000 1
000.10 1
000000 1
000.00 1
.00000 0
000010 1
000.10 1
0000.1 1
00.111 1

0.0001 1
0.0001 3
0.0010 1
0.0010 3
0.0100 1
0.0100 3
0.1000 1
0.1000 3
1.0000 1
1.0000 3
1.0001 1
1.0001 3
1.0010 1
1.0010 3
1.0100 1
1.0100 3
1.1000 1
1.1000 3
10.000 1
10.000 3
10.001 1
10.001 3
10.010 1
10.010 3
10.100 1
10.100 3
99.000 1
99.000 3
99.001 1
99.001 3
99.010 1
99.010 3
99.100 1
99.100 3
99.998 1
99.998 3




如果结果小于1 小数点前面是没用零的
这些过了应该不会wa了吧

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: