POJ 1001 Exponentiation 模拟小数幂

模拟小数幂

小数点位 pos

非零末位 e

长度 len

只有三种情况 

  pos > len

  pos < e

  e < pos < len

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 int a[500], b[500], c[500];
 5 void Cal()
 6 {
 7     memset(c, 0, sizeof(c));
 8     for(int i = 0; i < 200; i++)
 9         for(int j = 0; j < 10; j++)
10             c[i+j] += a[i] * b[j];
11     for (int i = 0; i < 200; i++){
12         c[i+1] += c[i]/10;
13         c[i] %= 10;
14     }
15     memset(a, 0, sizeof(a));
16     for (int i = 0; i <= 200; i++)
17         a[i] = c[i];
18 }
19 char s[10];
20 int pos, n, len;
21 int main()
22 {
23     while(cin>>s>>n)
24     {
25         memset(a,0,sizeof(a));
26         memset(b,0,sizeof(b));
27         memset(c,0,sizeof(c));
28         int t = 0;
29         for (int i = 5; i >= 0; i--)
30         {
31             if(s[i]==‘.‘) pos = i;
32             else b[t++] = s[i] - ‘0‘;
33         }
34         a[0] = 1;
35         for(int i = 1; i <= n; i++) Cal();
36         len = 200;
37         while(!c[len] && len>=0) len--;
38         int e = 0;//末尾
39         while(!c[e] && e<= len) e++;
40         pos = 5 - pos; pos *= n;//小数点
41         if(pos > len)
42         {
43             cout<<".";
44             for (int i = pos-1; i >= e; i--) cout<<c[i];
45             cout<<endl;
46         }
47         else if(pos <= e)//整数
48         {
49             for (int i = len; i >= pos; i--) cout<<c[i];
50             cout<<endl;
51         }
52         else//普通
53         {
54             for (int i = len; i >= pos; i--) cout<<c[i];
55             cout<<".";
56             for (int i = pos-1; i >= e; i--) cout<<c[i];
57             cout<<endl;
58         }
59     }
60 }
时间: 2024-10-13 11:29:42

POJ 1001 Exponentiation 模拟小数幂的相关文章

POJ 1001 Exponentiation 求高精度幂

Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 147507   Accepted: 36006 Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the n

POJ 1001 Exponentiation(JAVA,BigDecimal-&gt;String)

题目 计算实数a的n次方,具体输出格式看案例 import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); BigDecimal a = BigDecimal.ONE; BigDecimal ans = BigDecimal.ONE; int n; while(in.hasNextBi

POJ 1001 Exponentiation 无限大数的指数乘法 题解

POJ做的非常好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. 本题对程序健壮性的考查到达了变态级别了. 某人贴出的測试用例数据地址: http://poj.org/showmessage?message_id=76017 有了这些用例,几下调试就过了. 我关键漏了的用例: 000.10  1 000000  1 000.00  1 .00000  0 0000

POJ 1001 Exponentiation(大数幂,还是Java大发好!需调用多个方法)

Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 156303   Accepted: 38063 Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the n

POJ 1001 Exponentiation

Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 171995   Accepted: 41641 Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the n

hdu 1063 Exponentiation (高精度小数乘法)

//大数继续,额,要吐了. Problem 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 re

[ACM] POJ 1068 Parencodings(模拟)

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn

bestcoder#33 1001 高精度模拟

bestcoder#33 1001 高精度模拟 zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description As one of the most powerful brushes, zhx submits a lo

poj 3070 Fibonacci (矩阵快速幂求斐波那契数列的第n项)

题意就是用矩阵乘法来求斐波那契数列的第n项的后四位数.如果后四位全为0,则输出0,否则 输出后四位去掉前导0,也...就...是...说...输出Fn%10000. 题目说的如此清楚..我居然还在%和/来找后四位还判断是不是全为0还输出时判断是否为0然后 去掉前导0.o(╯□╰)o 还有矩阵快速幂的幂是0时要特判. P.S:今天下午就想好今天学一下矩阵乘法方面的知识,这题是我的第一道正式接触矩阵乘法的题,欧耶! #include<cstdio> #include<iostream>