hdoj 1063 Exponentiation 【大数精准次幂】

策略:将实数转化成整数, 记录下小数的位数, 最后的时候将前导0和后导0去掉输出就好了

ac by :SWS

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1063

代码:

#include<stdio.h>
#include<string.h>
int main()
{
	char s[10];
	int ans[250], n, i, j, d;
	while(scanf("%s %d", s, &n) == 2){
		int len =strlen(s);
		//while(len>0&&s[len-1] == '0') len--;
		int dc = 0, temp = 0; //dc是统计小数的位数
		i = 0;
		for(i = 0; i < len; i ++){
			if(s[i] == '.'){
				dc = len-i-1;
				continue;
			}
			else{
				temp = temp*10+s[i]-'0';
			}
		}
	//	printf("%d %d..\n", temp, dc);
		memset(ans, 0, sizeof(ans));
		ans[0] = 1;  //初始化为1
		 d = 1;
		for(j = 1; j <= n; j ++){
			int c = 0;
			for(i = 0; i < d; i ++){
				int s = ans[i]*temp+c;
				ans[i] = s%10;
				c = s/10;
			}
			while(c){
				ans[++d-1] = c%10;
				c/=10;
			}
		}
	//	for(i = 0; i < 250; i ++)
	//	printf("%d ", ans[i]);
		i= 0;
		while(i< dc*n&&ans[i] == 0) i++;  //去除小数点后面的无用0 这里有个条件i的范围是在小数点后面的
		j = 249;
		while(j >= dc*n&&ans[j] == 0) j --;  //去除前导0
	//	printf("%d %d\n", i, j);
		//d = j-dc*n;
		for(; j >=i; j --){
			if(j == (dc*n)-1) //判断是不是要输出小数点
			printf(".");
			printf("%d", ans[j]);
		}
		printf("\n");
	}
	return 0;
}


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值