[POJ] #1001# Exponentiation : 大数乘法

一. 题目
Exponentiation
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 156373 Accepted: 38086

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 R n 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

Hint

If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer
C++

while(cin>>s>>n)
{
...
}
c
while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want
/*while(scanf(%s%d",s,&n)!=EOF) //this also work */
{
...
}

Source

 
二. 题意
  • 给出字符串表示的浮点数 和 n次方数
  • 输出其n次方运算后的值
三. 分析
  • 算法核心:
    • 大数乘法:利用程序模拟算数运算过程,算出乘积
  • 实现细节:
    • 将输入数字从低位到高位反转存储,计算时方便进位
    • 模拟计算时,去除小数点,统一按照大整数相乘
    • 输出时逆序输出,同时计算小数点位置,相应位输出小数点
 
三. 题解
 1 #include <stdio.h>
 2 #include <memory.h>
 3 
 4 #define MAXN 100
 5 int a[MAXN], b[MAXN], len_a, len_b;
 6 
 7 /* 模拟算数运算过程,算出乘积 */
 8 void multiply(int *a, int *b)
 9 {
10     int i, j, len, c[MAXN];
11 
12     memset(c, 0, sizeof(c));
13 
14     for (i = 0; a[i] != -1; i++)
15         for (j = 0; b[j] != -1; j++)
16                 c[i + j] += a[i] * b[j];
17 
18     len = i + j - 1;
19 
20     for (i = 0; i < len; i++)
21         if (c[i] >= 10) { c[i + 1] += c[i] / 10; c[i] = c[i] % 10;}
22 
23     if (c[len] != 0) len_b = len + 1;
24     else len_b = len;
25 
26     for (i = 0; i < len_b; i++) b[i] = c[i];
27 }
28 
29 void main()
30 {
31     char str[MAXN];
32     int i, j, exp;
33     int st, pt, ed;
34 
35     freopen( "input.txt", "r" , stdin);
36 
37 
38 
39     while (scanf("%s %d", str, &exp) != EOF) {
40         memset(a, -1, sizeof(a));
41         memset(b, -1, sizeof(b));
42         len_a = 0;
43         st = pt = ed = -1;
44    
45      /*
46     1.st为输入数字的启始位置
47     2.pt为输入数字小数点的位置
48     3.ed为输入数字的结束位置
49     4.此三个中间变量的作用:
50       4.1 去掉开头和结尾的 0
51       4.2 对于小数点的特殊处理
52       4.3 用于计算出乘积后小数点的正确位置
53         */
54         for (i = 0; str[i] != '\0'; i++) {
55             len_a++;
56 
57             if (str[i] != '0' && str[i] != '.' && st == -1) st = i;
58             if (str[i] != '0' && str[i] != '.' || (str[i] != '.' && pt == -1)) ed = i;
59             if (str[i] == '.') pt = i;
60         }
61 
62         for (i = ed, j = 0; i >= st; i--) {
63             if (str[i] == '.') continue;
64 
65             b[j] = a[j] = str[i] - '0';
66             j++;
67         }
68 
69         len_b = len_a = j;
70         for(i = 1; i < exp; i++) multiply(a, b);
71 
72         if (pt < st) {
73             printf(".");
74             for (i = 0; i < (ed - pt) * exp - len_b; i++) printf("0");
75         }
76 
77         for (i = len_b - 1; i >= 0; i--) {
78             if (pt > st && pt < ed && i == (ed - pt) * exp - 1) printf(".");
79             printf("%d", b[i]);
80         }
81 
82         printf("\n");
83     }
84  }

 

转载于:https://www.cnblogs.com/yuanzone/p/5502805.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值