POJ1001-Precision power

全解题报告索引目录 -> 【北大ACM – POJ试题分类

转载请注明出处:http://exp-blog.com

-------------------------------------------------------------------------

 

 

浮点大数求幂,水题一道,把“大数乘浮点数”按指数循环就OK了,注意结果的整数部分若为0,则不保留整数部分。小数部分若为0,则不保留小数部分和小数点。

 

 

//Memory Time 
//1232K  0MS 

#include<iostream>
#include<string>
using namespace std;

const int size=1000;  //大数位数

void mult(char* A,char* B,char* ans)
{
	int i,j,k;

	int fract;   //总小数个数
	int dot=-1;  //小数点位置
	for(k=0;A[k]!='\0';k++)
		if(A[k]=='.')
			dot=k;
	int lena=k;
	if(dot==-1)
		fract=0;
	else
		fract=lena-dot-1;

	dot=-1;
	for(k=0;B[k]!='\0';k++)
		if(B[k]=='.')
			dot=k;
	int lenb=k;
	if(dot==-1)
		fract+=0;
	else
		fract+=(lenb-dot-1);  //总小数个数

	int a[size+1]={0};
	int b[size+1]={0};
	int pa=0,pb=0;

	/*倒序*/

	for(i=lena-1;i>=0;i--)
	{
		if(A[i]=='.')
			continue;
		a[pa++]=A[i]-'0';
	}
	for(j=lenb-1;j>=0;j--)
	{
		if(B[j]=='.')     //暂时删除小数点
			continue;
		b[pb++]=B[j]-'0';
	}

	int c[2*size+1]={0};
	int lenc;
	for(pb=0;pb<lenb;pb++)
	{
		int w=0;  //低位到高位的进位
		for(pa=0;pa<=lena;pa++)  // = 为了处理最后的进位
		{
			int temp=a[pa]*b[pb]+w;
			w=temp/10;
			temp=(c[pa+pb]+=temp%10);
			c[lenc=pa+pb]=temp%10;
			w+=temp/10;
		}
	}

	/*倒序,得到没有小数点的ans*/

	for(pa=0,pb=lenc;pb>=0;pb--)
		ans[pa++]=c[pb]+'0';
	ans[pa]='\0';
	lena=pa;

	 /*插入小数点*/

	bool flag=true; //标记是否需要删除小数末尾的0
	if(fract==0)   //小数位数为0,无需插入小数点
		flag=false;
	else if(fract<lena) //小数位数小于ans长度,在ans内部插入小数点
	{
		ans[lena+1]='\0';
		for(i=0,pa=lena;pa>0;pa--,i++)
		{
			if(i==fract)
			{
				ans[pa]='.';
				break;
			}
			else
				ans[pa]=ans[pa-1];
		}
				
	}
	else //小数位数大于等于ans长度,在ans前面恰当位置插入小数点
	{
		char temp[size+1];
		strcpy(temp,ans);
		ans[0]='0';
		ans[1]='.';
		for(int i=0;i<fract-lena;i++)  //补充0
			ans[i+2]='0';
		for(j=i,pa=0;pa<lena;pa++)
			ans[j++]=temp[pa];
		ans[j]='\0';
	}

	/*删除ans小数末尾的0*/

	if(flag)
	{
		lena=strlen(ans);
		pa=lena-1;
		while(ans[pa]=='0')
			ans[pa--]='\0';
		if(ans[pa]=='.')   //小数全为0
			ans[pa--]='\0';
	}

	/*删除ans整数开头的0,但至少保留1个0*/

	pa=0;
	while(ans[pa]=='0')  //寻找ans开头第一个不为0的位置
		pa++;

	if(ans[pa]=='\0')  //没有小数
	{
		ans[0]='0';
		ans[1]='\0';
	}
	else  //有小数
	{
		for(i=0;ans[pa]!='\0';i++,pa++)
			ans[i]=ans[pa];
		ans[i]='\0';
	}
	return;
}

char a[size+1];
char ans[size*size+1];

int main(void)
{
	int b;
	while(cin>>a>>b)
	{
		memset(ans,'\0',sizeof(ans));
		ans[0]='1';
		ans[3]='\0';

		for(int i=1;i<=b;i++)
			mult(a,ans,ans);

		cout<<ans<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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. 输入说明 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. 输出说明 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. 输入样例 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 输出样例 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 小提示 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 */ { ... } 来源 East Central North America 1988 北大OJ平台(代理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值