题目来源: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

T输入包括多组 R 和 n。 R 的值占第 1 到第 6 列,n 的值占第 8 和第 9 列。

Output

对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方。输出需要去掉前导的 0 后不要的 0
。如果输出是整数,不要输出小数点。

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】求高精度幂的更多相关文章

  1. Poj.Grids 2951 浮点数求高精度幂

    2951:浮点数求高精度幂 总时间限制: 1000ms 内存限制: 65536kB 描述 有一个实数 R ( 0.0 < R < 99.999 ) ,要求写程序精确计算 R 的 n 次方. ...

  2. 求高精度幂(java)

    求高精度幂 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 对数值很大.精度很高的数进行高精度计算是一类十分常见的问题.比如,对国债进行计算就是属于这类问题. 现在要 ...

  3. poj 1001 求高精度幂(Java, BigDecimal, pow, hasNext, stripTrailingZeros, toPlainString)

    求高精度幂 Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 180325   Accepted: 43460 Descripti ...

  4. Exponentiation(求高精度幂)

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 175340   Accepted: 42341 ...

  5. poj 1001 求高精度幂

    本题的测试用例十分刁钻,必须要考虑到很多的细节问题,在这里给出一组测试用例及运行结果: 95.123 12 548815620517731830194541.899025343415715973535 ...

  6. 求高精度幂(poj1001)

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

  7. ACM数论-快速幂

    ACM数论——快速幂 快速幂定义: 顾名思义,快速幂就是快速算底数的n次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高. 原理: 以下以求a的b次方来介绍: 把b转换成 ...

  8. 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 ...

  9. ACM | 算法 | 快速幂

    目录 快速幂 快速幂取模 矩阵快速幂 矩阵快速幂取模 HDU1005练习 快速幂 ​ 幂运算:\(x ^ n\) ​ 根据其一般定义我们可以简单实现其非负整数情况下的函数 定义法: int Pow ( ...

随机推荐

  1. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter cannot be cast to javax.servlet.Filter

    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter cannot be cast to javax.servle ...

  2. 剑指Offer——构建乘积数组

    题目描述: 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]. ...

  3. CF #301 A :Combination Lock(简单循环)

    A :Combination Lock 题意就是有一个密码箱,密码是n位数,现在有一个当前箱子上显示密码A和正确密码B,求有A到B一共至少需要滚动几次: 简单循环:

  4. shell awk实战

    一.文本处理 1.按行提取关键字频次(如取第5列) awk 'BEGIN{FS="|"} {a[$5]+=1;} END {for(i in a) print i ":& ...

  5. https通讯原理

    https通讯原理 HTTPS在传输数据之前需要客户端(浏览器)与服务端(网站)之间进行一次握手,在握手过程中将确立双方加密传输数据的密码信息.TLS/SSL协议不仅仅是一套加密传输的协议,更是一件经 ...

  6. ubuntu下30天自制os 的学习计划

    30天自制os的学习也告一段落,由于有其他更重要的事情要集中精力去处理.书本从15天開始就是多任务了.可是不得不停下一阵子. 以下总结下学习中遇到的一些问题. 1:学习这前14天中.问题最大的是关于G ...

  7. 009-Shell 函数

    一.函数定义 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; ...

  8. 【开发者笔记】解析具有合并单元格的Excel

    最近公司让做各种数据表格的导入导出,就涉及到电子表格的解析,做了这么多天总结一下心得. 工具:NOPI 语言:C# 目的:因为涉及到导入到数据库,具有合并单元格的多行必然要拆分,而NPOI自动解析的时 ...

  9. Spark2.0机器学习系列之6:GBDT(梯度提升决策树)、GBDT与随机森林差异、参数调试及Scikit代码分析

    概念梳理 GBDT的别称 GBDT(Gradient Boost Decision Tree),梯度提升决策树.     GBDT这个算法还有一些其他的名字,比如说MART(Multiple Addi ...

  10. strtoul函数的使用

    函数原型: unsigned long strtoul(const char *nptr,char **endptr,int base ) 参数1:字符串起始地址参数2:返回字符串有效数字的结束地址, ...