UVA748 Exponentiation进阶解法

#include<cstdio>
#include<cstring>

const int LENGTH = 701;
char F[10];
int n, base_number[LENGTH], power_number[LENGTH], temp_power_number[LENGTH], bit, length;

void transfer(void)//It's a function to refine input to get the non-demical part and bit of the result and the length of the non-demical result. 
{
	int i, j;
	for (i = 0, j = 0; i < 6; i++)
		if ('0' <= F[5 - i] && '9' >= F[5 - i])
		{
			base_number[j] = F[5 - i] - '0';
			j++;
		}
	for (i = 0; i < 6; i++)
		if ('.' == F[5 - i])
		{
			bit = i * n;
			break;
		}
	length = j;
}

void copy(int b[], int c[])//It's a function to copy all contains from array c to array b.
{
	for (int i = 0; i < LENGTH; i++)
		b[i] = c[i];
}

void accupower(void)//It's a function to get an accurate result of the power an integer.
{
	int i, j, k, Len = length * n;
	if (1 == n)
		copy(power_number, base_number);
	else
	{
		power_number[0] = 1;
		for (k = 0; k < n; k++)
		{
			for (i = 0; i < length; i++)
				for (j = 0; j < Len; j++)
				{
					temp_power_number[i + j] += base_number[i] * power_number[j];
					temp_power_number[i + j + 1] += temp_power_number[i + j] / 10;
					temp_power_number[i + j] %= 10;
				}
			copy(power_number, temp_power_number);
			memset(temp_power_number, 0, sizeof(temp_power_number));
		}
	}
}

void sudoprint(void)//It's a function to print output with the strict standard which bans pre-zeros, post-zeros, and dot of integer, etc. 
{
	int i, end = 0, flag;
	if (0 == power_number[0])
		for (i = 0; i < LENGTH; i++)
			if (0 != power_number[i])
			{
				end = i;
				break;
			}
	for (i = LENGTH - 1, flag = 0; i >= bit; i--)
		if (0 == flag && 0 != power_number[i])
		{

			flag = 1;
			printf("%d", power_number[i]);
		}
		else if (flag == 1)
			printf("%d", power_number[i]);
	if (0 != bit && i >= end)
	{
		printf(".");
		for (; i >= end; i--)
			printf("%d", power_number[i]);
	}
	printf("\n");
}

int main()//It's main function, the main part of the program.
{
	while (2 == scanf("%s%d", F, &n))
	{
		transfer();
		accupower();
		sudoprint();
		memset(power_number, 0, sizeof(power_number));
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值