整个题库的第二题,原本都没有屑于去做,突发奇想抱着秒杀的心态去写了代码,却硬生生地吃了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,整数和小数就可以统一处理了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int MAXN=+;
char s[MAXN];
int num[MAXN];
int ans[MAXN];
int n,ed,pos=; void switchnum()//将字符串转换为数字,并记录下小数点位置
{
bool have=false;
for (int i=;i<;i++)
{
if (s[i]=='.')
{
pos=(-i)*n;have=true;
}
else
{
if (have) num[-(i-)]=s[i]-'';
else num[-i]=s[i]-'';
}
}
if (have) num[]=;
for (int i=;i<;i++)
ans[i]=num[i];
} void mul()
{
int temp[MAXN];
memset(temp,,sizeof(temp));
for (int i=;i<=ed;i++)
for (int j=;j<=;j++)
{
temp[i+j]+=ans[i]*num[j];
if (i+j> && temp[i+j-]>)
{
temp[i+j]+=temp[i+j-]/;
temp[i+j-]%=;
}
}
ed=ed+;
if (temp[ed]>) //不要写成>10
{
ed++;
temp[ed]=temp[ed-]/;
temp[ed-]%=;
}
for (int i=;i<=ed;i++) ans[i]=temp[i];
} void output()
{
bool f=false;
int op=;
for (int i=;i<pos;i++) //整数部分末位的零不能删去
{
if (ans[i]>) break;
op++;
}
for (int i=ed;i>=op;i--)
{
if (i==pos-) //这里不能写成(i=pos-1)否则赋值,不要写成(i==pos+1)
{
cout<<'.';
f=true;
}
if (ans[i]>) f=true;
if (f) cout<<ans[i];
}
cout<<endl;
} int main()
{
while (scanf("%s%d",s,&n)!=EOF)
{
ed=; //除去小数点外的初始末位为5
switchnum();
for (int i=;i<n;i++) mul();
output();
}
}

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

  1. POJ-1001 Exponentiation 高精度算法

    题目链接:https://cn.vjudge.net/problem/POJ-1001 以前写过一个高精度乘法,但是没有小数点,实现起来也没什么难得, 现在把代码都般过来,等会把旧电脑弄一弄,暂时就不 ...

  2. 高精度POJ1001

    今天看到这道题了 poj1001 题目地址是http://bailian.openjudge.cn/practice/1001/ 英文看得懂,可是算法不明白,所以转别人的文章,留着给学生看看:乔高建( ...

  3. poj1001 Exponentiation【java大数】

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 183034   Accepted: 44062 ...

  4. poj1001 Exponentiation 大数的幂

    Description Problems involving the computation of exact values of very large magnitude and precision ...

  5. poj1001 Exponentiation

    Description Problems involving the computation of exact values of very large magnitude and precision ...

  6. POJ 1001 解题报告 高精度大整数乘法模版

    题目是POJ1001 Exponentiation  虽然是小数的幂 最终还是转化为大整数的乘法 这道题要考虑的边界情况比较多 做这道题的时候,我分析了 网上的两个解题报告,发现都有错误,说明OJ对于 ...

  7. poj1000-1009小结

    poj1000-1009小结 poj1000-1009小结 poj1000 AB poj1001 Exponentiation poj1002 poj1003 poj1004 Financial Ma ...

  8. 【PKU1001】Exponentiation(高精度乘法)

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 145642   Accepted: 35529 ...

  9. C# 高精度求幂 poj1001

    高精度求幂 public static char[] exponentiation(string a,int r) { ]; string b = ""; string c = a ...

  10. uva748 - Exponentiation 高精度小数的幂运算

    uva748 - Exponentiation   Exponentiation  Problems involving the computation of exact values of very ...

随机推荐

  1. pcap的安装

    pcap,即 packet capture library 抓包库,这个抓包库给抓包系统提供了一个高层次的接口.所有网络上的数据包,甚至是那些发送给其他主机的,通过这种机制,都是可以捕获的.它也支持把 ...

  2. python自动开发之第二十三天(Django)

    一.一大波model操作 1. 创建数据库表 # 单表 # app01_user ==> tb1 # users class User(models.Model): name = models. ...

  3. vue路由-基础

    安装 1.直接下载 / CDN https://unpkg.com/vue-router/dist/vue-router.js 在 Vue 后面加载 vue-router,它会自动安装的: <s ...

  4. SourceTree 过期,注册导入许可证

    参考这里:SourceTree过期,需要注册导入 SourceTree License 许可证 很详细 补充: 如果在 SourceTree 软件里注册失败,可以在网页注册. 如果其他邮箱不支持,可以 ...

  5. python实战===百度文字识别sdk

    http://ai.baidu.com/docs#/OCR-Python-SDK/top

  6. MySQL的数据库引擎的类型(转)

    腾讯后台开发电话面试问到数据库引擎选用的问题,这里补习一下. 本文属于转载,原文链接为:http://www.cnblogs.com/xulb597/archive/2012/05/25/251811 ...

  7. JQUERY 提取多个元素 a img 的 src href

    <div class="abc"><a href="1.html"><img src="1.jpg"/> ...

  8. Leetcode 之Longest Valid Parentheses(39)

    有一定的难度.用堆栈记录下所有左符的位置,用变量记录下孤立右符的位置. int longestValidParentheses(const string& s) { stack<int& ...

  9. 关于Free的override不能省略的问题,切记,虚方法是可以被覆盖的方法。

     

  10. ActiveMQ基于JMS的pub/sub传播机制

    原文地址:[ActiveMQ实战]基于JMS的pub/sub传播机制 发布订阅模型 就像订阅报纸,我们可以选择一份或者多份报纸.比如:北京日报.人民日报.这些报纸就相当于发布订阅模型中的topic.如 ...