【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 ( ...
随机推荐
- Mongodb系列文章
http://blog.csdn.net/congcong68 学习MongoDB 六: MongoDB查询(游标操作.游标信息)(三) Mongodb官方C#驱动示例 MongoDB Driver ...
- 与python的第一次邂逅
python简介 一.什么是python python是一种面向对象.直译式的计算机程序语言,所以有了武老师的那句名言:一切皆为对象 python的设计哲学是:“优雅”,“明确”,“简单” pytho ...
- Spring Data之Hello World
1. 概述 SpringData : 注意目标是使数据库的访问变得方便快捷;支持NoSQL和关系数据存储; 支持NoSQL存储: MongoDB(文档数据库) Neo4j(图形数据库) Redis(键 ...
- Junit 3.8.1 源码分析(一)
写在前面:本文基于Junit3.8.1版本,因为这是我第一次进行源码学习,先从简单的源码开始学起 1. 示例代码 1.1 准备工作 下载Junit3.8.1的JAR包 需要下载junit-3.8.1- ...
- 16.Update Methods-官方文档摘录
这里没什么好说的,直接贴文了 MongoDB provides the following methods for updating documents in a collection: db.col ...
- javaScript 调用构造函数 Array() 时没有使用参数, length总是0
如果调用构造函数 Array() 时没有使用参数,那么返回的数组为空,length 字段为 0. 当调用构造函数时只传递给它一个数字参数,该构造函数将返回具有指定个数.元素为 undefined 的数 ...
- ruamel.yaml 将字典写入yaml文件
#安装 pip install ruamel.yaml import os from ruamel import yaml # 将字典写入到yaml my_dic = { 'name': '张三', ...
- 文件传输(xmodem协议)
https://www.menie.org/georges/embedded/ 需要移植如下两个基础的硬件读写函数 int _inbyte(unsigned short timeout); void ...
- IPFS搭建&集群
下载go-ipfs wget https://github.com/ipfs/go-ipfs/releases/download/v0.4.17/go-ipfs_v0.4.17_linux-amd64 ...
- 二进制x&(x-1);
求下面函数的返回值(微软) int func(x) { int countx = 0; while(x) { countx ++; x ...