【高精度】POJ1001-Exponentiation

整个题库的第二题,原本都没有屑于去做,突发奇想抱着秒杀的心态去写了代码,却硬生生地吃了4个WA..

【思路】先去除掉小数点,进行最基本的高精度乘法运算,再在运算得到的结果中添加小数点输出。

【前铺】让我们先来看一看数组究竟需要设多大?数据范围是最大为99.999,则近似为100.000,当n=25时,至多有125个零,即最多占用125位。

【易错点】*数据1:10.000 01,如果直接从后往前去零的话,数据输出会变成1。所以删除末位多余零的范围是末位起,截止至小数点后。

     *数据2:000010 01,这个数据中根本不存在小数点!一开始我误以为6位中必然存在一位为小数点,所以将num的数组下标开为0..5,但这种情况下占用空间为0..6。我的解决方法是若当前数字存在小数点,则将其最后一位,即倒序摆放后的Num[0]设为0,整数和小数就可以统一处理了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using namespace std;
 6 const int MAXN=125+5;
 7 char s[MAXN];
 8 int num[MAXN];
 9 int ans[MAXN];
10 int n,ed,pos=0;
11
12 void switchnum()//将字符串转换为数字,并记录下小数点位置
13 {
14     bool have=false;
15     for (int i=0;i<7;i++)
16     {
17         if (s[i]==‘.‘)
18         {
19             pos=(6-i)*n;have=true;
20         }
21         else
22         {
23             if (have) num[5-(i-1)]=s[i]-‘0‘;
24                 else num[5-i]=s[i]-‘0‘;
25         }
26     }
27     if (have) num[0]=0;
28     for (int i=0;i<6;i++)
29         ans[i]=num[i];
30 }
31
32 void mul()
33 {
34     int temp[MAXN];
35     memset(temp,0,sizeof(temp));
36     for (int i=0;i<=ed;i++)
37         for (int j=0;j<=5;j++)
38         {
39             temp[i+j]+=ans[i]*num[j];
40             if (i+j>0 && temp[i+j-1]>9)
41             {
42                 temp[i+j]+=temp[i+j-1]/10;
43                 temp[i+j-1]%=10;
44             }
45         }
46     ed=ed+5;
47     if (temp[ed]>9)    //不要写成>10
48     {
49         ed++;
50         temp[ed]=temp[ed-1]/10;
51         temp[ed-1]%=10;
52     }
53     for (int i=0;i<=ed;i++) ans[i]=temp[i];
54 }
55
56 void output()
57 {
58     bool f=false;
59     int op=0;
60     for (int i=0;i<pos;i++)    //整数部分末位的零不能删去
61     {
62         if (ans[i]>0) break;
63         op++;
64     }
65     for (int i=ed;i>=op;i--)
66     {
67         if (i==pos-1)         //这里不能写成(i=pos-1)否则赋值,不要写成(i==pos+1)
68         {
69             cout<<‘.‘;
70             f=true;
71         }
72         if (ans[i]>0) f=true;
73         if (f) cout<<ans[i];
74     }
75     cout<<endl;
76 }
77
78 int main()
79 {
80     while (scanf("%s%d",s,&n)!=EOF)
81     {
82         ed=5;    //除去小数点外的初始末位为5
83         switchnum();
84         for (int i=1;i<n;i++) mul();
85         output();
86     }
87 }
时间: 2024-10-11 12:54:52

【高精度】POJ1001-Exponentiation的相关文章

poj1001 Exponentiation

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

poj1001 Exponentiation【java大数】

Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 183034   Accepted: 44062 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 解题报告 高精度大整数乘法模版

题目是POJ1001 Exponentiation  虽然是小数的幂 最终还是转化为大整数的乘法 这道题要考虑的边界情况比较多 做这道题的时候,我分析了 网上的两个解题报告,发现都有错误,说明OJ对于错误的判断还不够严厉. 对边界情况的讨论其实应该是思维严密的表现,当然这并不能表明我写的一点错误都没有,只是多多分析一下还是很有好处的. #include <iostream> #include <fstream> #include <string> #include &l

poj1001(高精度)

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

poj1001(高精度乘法)

1.题目表述 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 135893   Accepted: 33256 Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation o

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

【PKU1001】Exponentiation(高精度乘法)

Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 145642   Accepted: 35529 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 求高精度幂

[题目描述] 给定R与n,求Rn的精确值,其中(0.0<R<99.99, n为整数0<n<=25). [思路分析] 1. 存储结构 由于R最大不超过100,n不会超过25,故Rn不会超过50位,保险起见采用100位的int数组.数组采用与字符串的逆序方式存储,即str=”123456”中str[0]=’1’而存入int数组后,int[0]=’6’.因此,在输入时候,可以通过字符串的字符个数以及是否存在小数点来判断这个数字的位数,根据位数从最高位(str[0])开始存储数字.这样虽然

Exponentiation(高精度)

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

C# 高精度求幂 poj1001

高精度求幂 public static char[] exponentiation(string a,int r) { char[] aa = new char[1]; string b = ""; string c = a; for (int i = 0; i < r-1; i++) { aa = acm.Quadrature(c, a); b = ""; foreach (var item in aa) { b += item; } c = b; } re