【高精度】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,整数和小数就可以统一处理了。
#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的更多相关文章
- POJ-1001 Exponentiation 高精度算法
题目链接:https://cn.vjudge.net/problem/POJ-1001 以前写过一个高精度乘法,但是没有小数点,实现起来也没什么难得, 现在把代码都般过来,等会把旧电脑弄一弄,暂时就不 ...
- 高精度POJ1001
今天看到这道题了 poj1001 题目地址是http://bailian.openjudge.cn/practice/1001/ 英文看得懂,可是算法不明白,所以转别人的文章,留着给学生看看:乔高建( ...
- poj1001 Exponentiation【java大数】
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 183034 Accepted: 44062 ...
- poj1001 Exponentiation 大数的幂
Description Problems involving the computation of exact values of very large magnitude and precision ...
- poj1001 Exponentiation
Description Problems involving the computation of exact values of very large magnitude and precision ...
- POJ 1001 解题报告 高精度大整数乘法模版
题目是POJ1001 Exponentiation 虽然是小数的幂 最终还是转化为大整数的乘法 这道题要考虑的边界情况比较多 做这道题的时候,我分析了 网上的两个解题报告,发现都有错误,说明OJ对于 ...
- poj1000-1009小结
poj1000-1009小结 poj1000-1009小结 poj1000 AB poj1001 Exponentiation poj1002 poj1003 poj1004 Financial Ma ...
- 【PKU1001】Exponentiation(高精度乘法)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 145642 Accepted: 35529 ...
- C# 高精度求幂 poj1001
高精度求幂 public static char[] exponentiation(string a,int r) { ]; string b = ""; string c = a ...
- uva748 - Exponentiation 高精度小数的幂运算
uva748 - Exponentiation Exponentiation Problems involving the computation of exact values of very ...
随机推荐
- bzoj 1034 贪心
首先如果我们想取得分最高的话,肯定尽量赢,实在赢不了的话就耗掉对方最高的,那么就有了贪心策略,先排序,我方最弱的马和敌方最弱的相比,高的话赢掉,否则耗掉敌方最高的马. 对于一场比赛,总分是一定的,所以 ...
- canvas_简单练习
效果图 实现原理: 1.定义canvas标签. 2.获取canvas标签节点,创建canvas2D. 3.在canvas进行画图. 效果代码: <!DOCTYPE html> <ht ...
- upupw注入by pass
http:' and updatexml(null,concat(0x5c,(/*!00000select SCHEMA_name*/from/*!information_schema*/.schem ...
- Python模块学习 - ConfigParser
配置文件 很多软件都用到了配置文件,像git运行的时候会读取~/gitconfig,MySQL运行的时候会读取/etc/my.cnf,Python 提供的包管理工具pip命令,也会去读取~/.pip/ ...
- python之计算器
开发一个简单的python计算器 1.实现加减乘除及拓号优先级解析 2.用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * ...
- Python Matplotlib图表汉字显示成框框的解决办法
http://blog.sina.com.cn/s/blog_662dcb820102vu3d.html http://blog.csdn.net/fyuanfena/article/details/ ...
- 网络知识===关于MAC地址和IP不能互相替代,缺一不可的原因
最近在看书<图解TCP/IP>书中分别谈到了IP和MAC地址.于是我就有两个疑惑, 为什么有了IP地址,我们还要获取MAC地址? 为什么我们初始不直接使用MAC地址作为终点地址?还要那么复 ...
- 2017多校第5场 HDU 6085 Rikka with Candies bitset
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6085 题意:存在两个长度为n,m的数组A,B.有q个询问,每个询问有一个数字k,可以得到Ai%Bj=k ...
- 解析jsp的 tomcat 、resin
一.tomcat 1. 安装JDK [root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://www.lishi ...
- 初学jmeter
jmeter安装的前提需要有jdk环境,下载安装好jdk环境后记得要设置好环境变量. 配置环境变量:右击“我的电脑”-->"高级"-->"环境变量" ...