首页 > 代码库 > 【POJ】 1001-Exponentiation

【POJ】 1001-Exponentiation

题目:

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

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

思路:

把输入全部转换成整数,剔除小数点,按整数高精度运算后再加入小数点

代码:

//21:10  --  23:11
#include <stdio.h>
#include <string.h>

char res[10006], sa[10006], sb[10006], tsb[10006];

void show(char *s)
{
	char *p;
	if (*s == 0 && *(s + 1) == -2)
		++s;
	if (*s == -2 && *(s + 1) == -1)
	{
		printf("0");
		return;
	}
	
	p = s;
	while (*++p != -1)
		;
	--p;
	while (*p != -2 && *p == 0)
		--p;
	if (*p == -2)
		--p;
	++p;
	
	while (s != p)
	{
		if (*s == -2)
			putchar('.');
		else
			putchar('0' + *s);
		++s;
	}
}
void pre(char *s)
{
	while (*s != '\0')
	{
		if (*s == '.')
			*s = -2;
		else
			*s -= '0';
		++s;
	}
	*s = -1;
}
void owncpy(char *s, char *p)
{
	while (*p != -1)
	{
		*s++ = *p++;
	}
	*s = -1;
}
char* multiple(char *s, char *a, char *b)
{
	memset(s, 0, sizeof(char) * 10006);
	int i, flags = 0, lena = 0, lenb = 0, ea, eb;
	for (i = 0; a[i] != -1; ++i)
	{
		if (flags == 1)
		{
			a[i - 1] = a[i];
			lena++;
		}
		if (a[i] == -2)
		{
			flags = 1;
			lena = 0;
		}
	}
	ea = flags ? i - 2 : i - 1;
	flags = 0;
	for (i = 0; b[i] != -1; ++i)
	{
		if (flags == 1)
		{
			b[i - 1] = b[i];
			lenb++;
		}
		if (b[i] == -2)
		{
			flags = 1;
			lenb = 0;
		}
	}
	eb = flags ? i - 2 : i - 1;

	int x, y, c;
	s[10001] = -1;
	for (x = ea; x >= 0; --x)
	{
		for (y = eb; y >= 0; --y)
		{
			c = 10000 - (ea - x + eb - y);
			s[c] += a[x] * b[y];
			if (s[c] > 9)
			{
				int tp = s[c] / 10;
				s[c] %= 10;
				s[c - 1] += tp;
			}
		}
	}

	int first = 10000 - (ea + eb + 5);
	while (first <= 10000 && s[++first] == 0)
		;
	int f = 0, ploc;
	if (lena + lenb >= 10000 - first + 1)
	{
		s[f++] = 0;
		s[f++] = -2;
		int k = lena + lenb - (10000 - first + 1);
		while (k--)
			s[f++] = 0;
	}
	else
	{
		ploc = 10000 - lena - lenb;
		while (first <= ploc)
			s[f++] = s[first++];
		s[f++] = -2;
	}
	
	while (first <= 10000)
		s[f++] = s[first++];
	s[f] = -1;
	return s;
}
void ownpow(int n)
{
	while (--n)
	{
		multiple(res, sa, sb);
		owncpy(sa, res);
		owncpy(sb, tsb);
	}
}
int main()
{
	int n;
	while (~scanf("%s %d", sa, &n))
	{
		pre(sa);
		owncpy(sb, sa);
		owncpy(tsb, sa);
		owncpy(res, sa);
		ownpow(n);
		show(res);
		printf("\n");
	}
	return 0;
}


【POJ】 1001-Exponentiation