【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 ( ...
随机推荐
- 剑指Offer——二叉树的深度
题目描述: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 分析: 二叉树的深度等于其左子树的深度和右子树的深度两个中最大的深 ...
- 使用python的logging模块(转)
一.从一个使用场景开始 开发一个日志系统, 既要把日志输出到控制台, 还要写入日志文件 import logging # 创建一个logger logger = logging.getLogger(' ...
- LRU算法的Python实现
http://flychao88.iteye.com/blog/1977653文章中介绍了常见的几种缓存淘汰策略 LRU:least recently used,最近最少使用算法.其实就是按使用时间倒 ...
- first-child与:first-of-type的区别
css选择器中:first-child与:first-of-type的区别 :first-child选择器是css2中定义的选择器,从字面意思上来看也很好理解,就是第一个子元素.比如有段代码: p:f ...
- C#的Installer生成的msi的安装文件,安装新版本时提示:已经安装了该产品的另一个版本。无法继续安装此版本
之前折腾了个C#的项目: WLW (Windows Live Writer) Plugin–InsertSkydriveFiles 然后又弄了个对应的Installer: [已解决]给一个C#的Dll ...
- Python之OS模块函数
函数列表: 1 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'. os. ...
- 《浅谈CT》总结
注明来自 http://www.ssdfans.com/?p=1941 这里说的CT,不是医院里面的CT,而是闪存的一种技术:Charge Trap. 闪存不只有Floating Gate,还有Cha ...
- matplotlib绘制饼状图
源自http://blog.csdn.net/skyli114/article/details/77508430?ticket=ST-41707-PzNbUDGt6R5KYl3TkWDg-passpo ...
- STL vector 内存释放
最近在论坛看到一个提问帖子,问题是vector中存储了对象的指针,调用clear后这些指针如何删除? class Test { public: Test() {} ~Test() { cout < ...
- 磁盘结构,平均寻道时间,平均延迟时间,虚拟内存与MMU
首先了解一下磁盘:磁盘低速的原因是因为它一种机械装置,在磁盘中有一个或多个金属盘片,它们以5400,7200或10800rpm(RPM =revolutions per minute 每分钟多少转 ) ...