【ACM】求高精度幂
题目来源:http://poj.org/problem?id=1001&lang=zh-CN
| Time Limit: 500MS | Memory Limit: 10000K | |
| Total Submissions: 160807 | Accepted: 39157 |
Description
现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n
次方(Rn),其中n 是整数并且 0 < n <= 25。
Input
Output
。如果输出是整数,不要输出小数点。
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 求解思路:
1、使用数组存储数据,保证数组的元素不大于9,不存储小数点,通过一个整型变量记录小数位的位数。用string接受输入流的数据,再转换成整型数组(不含“.”),作为第一次乘法计算的一个因子,并生成一个整数作为另一个因子。
2、核心函数是 void multiplicationCompute(vector<int> &ivecA, unsigned int &pointPosA, const int &intR, const int &pointPosR),计算数组存储的大整数与底数的乘积。
为了方便计算,大整数存储在数组的高位— —低位。计算的时候,从数组低位— —高位开始遍历,每一位数组元素(非-1占位符)与底数相乘,并将结果依次存入数组。对于ivecA[index] 与底数的乘积,其个位存入index位,其更高位依次存入index--位。循环调用函数计算,其最终结果存储在ivecA中。
3、输出控制。整数不输出小数点;小数位后缀0不输出;纯小数不输出小数点前面的0;如果ivecA存储的实数位小于小数位,则需要在前面补0。 不过很惭愧的是下面的代码提交未能通过。在本机测试结果都正确,但是提交结果却是wrong answer,一时半会也没找到错误原因,待回头再来看吧。
#include <iostream>
#include <string>
#include <vector> using namespace std; // Element == -1 ? reset 0 !
inline void resetElement(int &ival)
{
if (ival == -)
{
ival = ;
}
} // Element > 10 ? deal element!
inline void dealElement(vector<int> &ivec, vector<int>::size_type index)
{
while (ivec[index] >= )
{
int temp = ivec[index];
ivec[index] %= ;
index--;
resetElement(ivec[index]);
ivec[index] += (temp / );
}
} // ivecR[index] * intR
void multiplicationCompute(vector<int> &ivecA, unsigned int &pointPosA, const int &intR, const int &pointPosR)
{
vector<int>::size_type currIndex = ;
unsigned int val = ; for (vector<int>::size_type index = ; index != ivecA.size(); ++index)
{
if (ivecA[index] != -)
{
currIndex = index;
val = ivecA[index] * intR; ivecA[index] = ; // += while (val)
{
resetElement(ivecA[currIndex]);
ivecA[currIndex] += (val % );
dealElement(ivecA, currIndex);
val /= ;
currIndex--;
}
}
}
pointPosA = pointPosA + pointPosR;
} int main(int argc, char *argv[])
{ string strR;
unsigned int n; while (cin >> strR >> n)
{
unsigned int intR = ;
vector<int> ivecR;
vector<int> ivecA(, -);
unsigned int pointPositionR = ;
unsigned int pointPositionA = ; //将R转换为int型的vector,pointPositionR记录小数点位置(其值代表小数位的位数)
for (string::size_type index = ; index != strR.size(); ++index)
{
if (strR[index] != '.')
{
ivecR.push_back(strR[index] - '');
}
else
{
pointPositionR = strR.size() - - index;
}
} //将ivecR转换成intR
for (vector<int>::size_type index = ; index != ivecR.size(); ++index)
{
intR = intR * + ivecR[index];
} //将ivecR复制到ivecA,高位——低位存储,pointPositionA = pointPositionR
for(int indexA = ivecA.size() - , indexR = ivecR.size() - ; indexA >= && indexR >= ; --indexA, --indexR)
{
ivecA[indexA] = ivecR[indexR];
}
pointPositionA = pointPositionR; //if (n = 0)
//{
// cout << 1 << endl;
// return 0;
//} while (n >= ) //若 n = 1, 则 ivecA就是结果
{
multiplicationCompute(ivecA, pointPositionA, intR, pointPositionR);
n--;
} //纯小数,小数位超过ivecA的实数位则补0
for (vector<int>::size_type index = ivecA.size() - ; index >= ivecA.size() - pointPositionA; --index)
{
if (ivecA[index] == -)
{
ivecA[index] = ;
} } //后缀0处理
for (int index = ivecA.size() - ; index >= && ivecA[index] == ; --index)
{
ivecA[index] = -;
} vector<int>::size_type indexBegin = ;
while (indexBegin != ivecA.size() && ivecA[indexBegin] == -)
{
indexBegin++;
} if (indexBegin == ivecA.size())
{
cout << << endl;
}
else
{
for(vector<int>::size_type index = indexBegin; index != ivecA.size(); ++index)
{
if (ivecA[index] != -)
{
if (index == ivecA.size() - pointPositionA)
{
cout << ".";
}
cout << ivecA[index];
}
}
cout << endl;
}
}
return ;
}
【ACM】求高精度幂的更多相关文章
- Poj.Grids 2951 浮点数求高精度幂
2951:浮点数求高精度幂 总时间限制: 1000ms 内存限制: 65536kB 描述 有一个实数 R ( 0.0 < R < 99.999 ) ,要求写程序精确计算 R 的 n 次方. ...
- 求高精度幂(java)
求高精度幂 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 对数值很大.精度很高的数进行高精度计算是一类十分常见的问题.比如,对国债进行计算就是属于这类问题. 现在要 ...
- poj 1001 求高精度幂(Java, BigDecimal, pow, hasNext, stripTrailingZeros, toPlainString)
求高精度幂 Time Limit: 500MS Memory Limit: 10000K Total Submissions: 180325 Accepted: 43460 Descripti ...
- Exponentiation(求高精度幂)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 175340 Accepted: 42341 ...
- poj 1001 求高精度幂
本题的测试用例十分刁钻,必须要考虑到很多的细节问题,在这里给出一组测试用例及运行结果: 95.123 12 548815620517731830194541.899025343415715973535 ...
- 求高精度幂(poj1001)
Description Problems involving the computation of exact values of very large magnitude and precision ...
- ACM数论-快速幂
ACM数论——快速幂 快速幂定义: 顾名思义,快速幂就是快速算底数的n次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高. 原理: 以下以求a的b次方来介绍: 把b转换成 ...
- HDU 1402 A * B Problem Plus (FFT求高精度乘法)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- ACM | 算法 | 快速幂
目录 快速幂 快速幂取模 矩阵快速幂 矩阵快速幂取模 HDU1005练习 快速幂 幂运算:\(x ^ n\) 根据其一般定义我们可以简单实现其非负整数情况下的函数 定义法: int Pow ( ...
随机推荐
- git 学习(2)--恢复版本
查看修改历史记录 $ git log commit fba77877d316436c1b774b8933380ebcac668040 Author: keith <ustbfxx@163.com ...
- andriod的apk文件相关的编译反编译工具
1.smali-1.2.6.jar 用途:.smali文件 转成 classes.dex文件 说明:.smali文件,类似于.class文件,可以用普通文本编辑器查看和修改. 用法举例:命令行:jav ...
- c#自定义控件窗体Click无法点击Lable的处理解决方案
自定义控件做按钮,不继承Button,用Lable来做按钮文字时,点击空白处有效,但是点击lable不起作用的处理方案. 很简单,就是在Lable添加Click事件,事件中添加代码:OnClick(e ...
- css calc()
w https://developer.mozilla.org/en-US/docs/Web/CSS/calc The calc() CSS function can be used anywhere ...
- The Personal Touch Client Identification 个性化接触 客户识别
w服务器要知道和谁在交谈. HTTP The Definitive Guide Web servers may talk to thousands of different clients simul ...
- QT in VS 多语言实现(中英文切换,每个步骤都有截图,只有UTF8才能让Qt语言家正确读取。先qApp->removeTranslator,然后installTranslator,每个类都要写上槽函数RetranslateUI)
最近项目需要软件具有中英文双语切换功能,而QT又自带此功能,现将实现方式记录下来. 说到中英文切换,少不了要了解QT的内部编码方式.在此就不详述QT编码方式了,具体可参考 彻底弄懂Qt的编码.只需要记 ...
- Kafka简介及使用
一.Kafka概述 离线部分: Hadoop->离线计算(hdfs / mapreduce) yarn zookeeper->分布式协调(动物管理员) hive->数据仓库(离线计算 ...
- Linux内核中namespace之PID namespace
前面看了LInux PCI设备初始化,看得有点晕,就转手整理下之前写的笔记,同时休息一下!!~(@^_^@)~ 这片文章是之前写的,其中参考了某些大牛们的博客!! PID框架的设计 一个框架的设计会考 ...
- GNU Screen使用入门
前些天开始学习使用GNU Screen程序,发现这个工具在管理服务器时候确实挺方便的,于是写一篇文章总结一下,顺便介绍Screen的基本使用方法. 简介 GNU Screen是 一个基于文本的全屏窗口 ...
- Jacl 是 TCL 的一个备用实现
Jacl 是 TCL 的一个备用实现,它是完全使用 Java 代码编写的. wsadmin 工具使用 Jacl V1.3.2. 建议不要在 wsadmin 工具中使用 Jacl 语法 建议不要使用一个 ...