Exponentiation(C语言)

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

将 小数放大转化成为小数再按大数计算, 输出的时候加上 . 即可

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
	char R[10];//将浮点数以字符串的形式存入
	int n;
	int num[3000];//将计算好的数用num数组来储存
	int i, j, k, count, flat, sum, l, glat;
	while (scanf("%s%d", R, &n) != EOF)
	{
		memset(num, 0, sizeof(num));
		int len = strlen(R);
		count = 0, flat = 0, sum = 0, glat = 0;
		for (i = 0; i < len; i++)//检查输入的是否为小数,是的话就标记
			if (R[i] == '.')
			{
				flat = 1;
				break;
			}
		if (flat == 1)//如果是小数且不属于100.000之类的就进行标记
		{
			for (l = i + 1; l < len; l++)
			{
				if (R[l] != '0')
					glat = 1;
			}
		}
		if (glat == 0)//否则就将小数按整数处理
		{
			flat = 0;
			len = i;
		}
		int p;
		if (flat)//如果是小数就将其放大转化为整数并加入sum中
		{
			count = len - i - 1;
			p = len - 2;
			for (j = 0; j < len; j++)
			{
				if (j == i)
					continue;
				sum += (R[j] - '0') * pow(10, p--);
			}
		}
		else//否则直接加入到sum中
		{
			p = len - 1;
			for (j = 0; j < len; j++)
				sum += (R[j] - '0') * pow(10, p--);
		}
		num[0] = 1;
		for (i = 1; i <= n; i++)//模拟大数乘法, 将每一位存入数组
		{
			int c = 0;
			for (j = 0; j < 3000; j++)
			{
				int s = num[j] * sum + c;
				num[j] = s % 10;
				c = s / 10;
			}
		}
		for (j = 2999; j >= 0; j--)//去掉前面没用的零
			if (num[j] || j == count * n - 1)
				break;
		int k = 0, glat = 0;
		if (flat)//如果是小数要去掉后面没有意义的零
		{
			for (i = 0; i <= j; i++)
				if (num[i])
				{
					glat = i;
					break;
				}
		}
		for (i = j; i >= glat; i--)//将计算的这个数输出
		{
			k++;
			if (i == count * n - 1 && flat)
				printf(".");
			printf("%d", num[i]);
		}
		printf("\n");
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值