Peck Chen

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

暑假做的, 高精度大数

贴一下

哈哈...

Accepted    148K    0MS    C    2729B    2010-11-18 21:54:09

 

 

代码
#include <stdio.h>
#include
<stdlib.h>
#include
<string.h>

char str[6]; int n, sum[200], result[200];

/* 求次方 */
void nInteMultiple(int num[], int len1, int n, int tag)
{
int i, j, len2, index = n, storage = 0, temp = 0;

memset(sum,
0, sizeof(sum)); sum[0] = 1;
while ( index-- ) //n次相乘
{
/* i = len1 - 1; j = 0; //两个数开始相乘时的位置, len1为num的长度,同理len2 */
memset(result,
0, sizeof(result));
len2
= 199;
while (!sum[len2]) --len2;
len2
+= 1; /* sum的长度; */

for (i = len1-1; i >= 0; i--) /* sum与num相乘一次 */
{
for (j = 0; j < len2; j++)
{
temp
= num[i];
result[len1
-i-1+j] += (sum[j] * temp);
}
}
for (i = 0; i < 199; i++) /* 处理进位 */
{
storage
= result[i];
result[i]
= storage % 10;
result[i
+1] += (storage / 10);
}
for (i = 0; i < 200; i++)
{
sum[i]
= result[i];
}
}

i
= 199;
while (!result[i]) --i; /* 寻找后导第一个不为零的位置 */
if (i+1 <= tag) /* 输出的数为.000XXXXXXXXX */
{
printf(
".");
for (j = 1; j <= (tag-i-1); j++)
{
printf(
"0");
}
while (i >= 0) printf("%d", sum[i--]);
}
while (i >= 0) /* 开头不是以0.000XXXX的数的输出 */
{
if (!tag)
{
printf(
"%d", sum[i--]);
}
else
{
/* 可能小数位数比所得的结果的位数多刚要输出前辍零 */
if (i == tag-1) printf(".");
printf(
"%d", sum[i--]);
}
}
printf(
"\n");
}

/* 数据处理和调用求次方函数 */
void start(char *str, int n) /* 去早数点后的合法整数和最终小数位数 */
{
int *num;
int i = 0, j = 0, index = 0, dot = 0, tag, len, x, y; /* index为返回的整形数组的长度 */

len
= strlen(str); /* 字符数组的长度 */
i
= len - 1;
while (str[i] == '0') --i; /* 去后导零后的位置 */
while (str[j] == '0') ++j; /*去前导零后的位置 */
x
= j; y = i; /* 保存j 和 i; */

/* 最终小数点后的位数 */
index
= i - j + 1; //去掉前导零和后辍零后的位数(可能包含小数点)
for (; j <= i; j++)
{
if (str[j] == '.')
{
dot
= 1; /* 有小数点 */
index
--; /* 去数点后的合法数字的长度 */
tag
= (i-j) * n; /* 小数点后最终的位数 */
break;
}
}
if (j > i)
{
dot
= 0; /* 没有小数点 */
tag
= 0;
}
/* 所得范围的数字可能是:000XXXX,要将零去掉 */
if (str[x] == '.')
{
++x; /* 有小数点时index已减1; */
while (str[x] == '0')
{
--x;
index
--;
}
}
/* 将有效字符转换为整型数组 */
i
= 0;
if ((num=(int*)malloc((index) * sizeof(int))) == NULL)
{
/* 动态申请数组 */
printf(
"空间分配失败!\n");
exit(
-1);
}
if (!dot) /* 没有小数点 */
{
for (; x <= y; x++)
{
num[i]
= str[x] - '0';
i
++;
}
}
else /* 有小数点 */
{
for (; x <= y; x++)
{
if (str[x] != '.')
{
num[i]
= str[x] - '0';
i
++;
}
}
}
/* 去小数点后的合法数字已在存储在sum中, 长度为index,小数为tag-----界限以上 */
nInteMultiple(num, index, n, tag);
//求次方

free( num );
/* 清理资源 */
num
= NULL; /* 指针置空 */
}

int main()
{
while (scanf("%s%d", str, &n) != EOF)
{
start(str, n);
}
return 0;
}

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

posted on 2010-11-18 21:59  PeckChen  阅读(332)  评论(0编辑  收藏  举报