POJ 1001 Exponentiation

时限:500 ms   内存限制:10000 K
提交材料共计: 179923   接受: 43369

描述:求得数R( 0.0 < R < 99.999 )的n( 0 < n <= 25 )次方的精确值

样本输入

95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12

样本输出

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

分析:题中R的n次方远大于double的范围,那么就会想到用数组来存放结果,题目就变成了两个非常大的数相乘,小数相乘可以先把小数点去除,先算出小数点在结果中的位置,再直接计算两个整数相乘的值。

举例:95.123 12

求95.123的12次方的值,即先去除小数点,计算出小数点的位置(3 * 12 = 36), 再把95123相乘12次得出结果,输出时再插入小数点。

代码:

#include<iostream>
#include<cstring>
#include<string>
using namespace std;
const int N = ;
int result[N];
int temp[N];
int main() {
string str;
int n;
while(cin >> str >> n) {
for(int i = ; i < N; i++) result[i] = ;
result[] = ;
int m = ;
int cs[] = {};
int pos;
for(int i = str.size() - ; i >= ; i--) { //去除小数点
if(str[i] != '.') cs[m++] = str[i] - '';
else pos = i;
}
int point = ( - pos)*n;
for(int i = ; i < n; i++) {
for(int j = ; j < N; j++) temp[j] = ;
for(int j = ; j < ; j++) {
if(!cs[j]) continue; //如果乘数为0,那么无需向下执行
int c;
for(int k = ; k < N; k++) { //加到对应的位数上
c = result[k] * cs[j];
temp[k+j] += c;
}
for(int k = ; k < N; k++) { //处理进位
if(temp[k] >= ) {
temp[k+] += temp[k]/;
temp[k] %= ;
}
}
}
for(int j = ; j < N; j++) result[j] = temp[j]; //转存
}
int l = ;
while(!result[l] && l < point) l++;
int r = N - ;
while(!result[r] && r >= point) r--;
for(int i = r; i >= point; i--) cout << result[i];
if(l < point) cout << "."; //如果的小数,那么输出小数点
for(int i = point - ; i >= l; i--) cout << result[i];
cout << endl;
}
return ;
}

POJ 1001 Exponentiation(大数运算)的更多相关文章

  1. [POJ] #1001# Exponentiation : 大数乘法

    一. 题目 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 156373   Accepted: ...

  2. [POJ 1001] Exponentiation C++解题报告 JAVA解题报告

        Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 126980   Accepted: 30 ...

  3. POJ 1001 Exponentiation 无限大数的指数乘法 题解

    POJ做的非常好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. ...

  4. poj 1001 Exponentiation 第一题 高精度 乘方 难度:1(非java)

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 138526   Accepted: 33859 ...

  5. POJ 1001 Exponentiation(JAVA,BigDecimal->String)

    题目 计算实数a的n次方,具体输出格式看案例 import java.util.*; import java.math.*; public class Main { public static voi ...

  6. POJ 1001 Exponentiation

    题意:求c的n次幂……要求保留所有小数…… 解法:一开始只知道有BigInteger……java大数+模拟.第一次写java大数……各种报错各种exception……ORZ 没有前导0和小数后面的补位 ...

  7. 1001. Exponentiation高精度运算总结

    解题思路 这道题属于高精度乘法运算,要求输入一个实数R一个指数N,求实数R的N次方,由于R有5个数位,而N又特别大,因此用C++自带的数据类型放不下. 解题思路是通过数组储存每次乘积结果和底数的每一位 ...

  8. POJ 1001 Exponentiation 模拟小数幂

    模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...

  9. HOJ 2148&POJ 2680(DP递推,加大数运算)

    Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...

随机推荐

  1. 服务器不能设置内容类型HTTP头信息后发送

    昨天一个用户反映,导出excel失败,我测试了一下,数据量小没有问题,数据量稍微大就会出现这个问题.咨询度娘也没有找到合适的解决方法,突然想到系统中,其他模块有下载excel附件的功能,但是从没有出现 ...

  2. 设计模式(三)Singleton Pattern单例设计模式

    1.饿汉式 public class SingletonDemo { private static SingletonDemo s=new SingletonDemo(); private Singl ...

  3. Thread.Sleep(0)妙用

    Thread.Sleep(0)妙用 我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题: 假设现在是 2008-4-7  ...

  4. Android 关于虹软人脸识别SDK引擎使用总结

    虹软 最近开放了人脸识别的SDK引擎(免费的哦),刚好有Android版的,就体验了一波.下面来说说Android版的SDK使用心得: ArcFace 虹软人脸认知引擎简介 目前开放的版本有人脸比对( ...

  5. angular2 脏检查机制

    https://www.waitig.com/angular2-%E8%84%8F%E6%A3%80%E6%9F%A5%E8%BF%87%E7%A8%8B.html https://zhuanlan. ...

  6. Asp.net core 学习笔记 ( identity server 4 JWT Part )

    更新 : id4 使用这个 DbContext 哦 dotnet ef migrations add identity-server-init --context PersistedGrantDbCo ...

  7. 第 5 章 网络 - 031 - none和host网络的适用场景

    none和host网络的适用场景 Docker 安装时会自动在 host 上创建三个网络,用 docker network ls 命令查看: root@ubuntu:~# docker network ...

  8. C#打印格式

    一:C#代码直接打印pdf文件(打印质保书pdf文件) 引用: 代码注释很详细了. private void btn_pdf_Click(object sender, RoutedEventArgs ...

  9. highly variable gene | 高变异基因的选择 | feature selection | 特征选择

    在做单细胞的时候,有很多基因属于noise,就是变化没有规律,或者无显著变化的基因.在后续分析之前,我们需要把它们去掉. 以下是一种找出highly variable gene的方法: The fea ...

  10. 20170907wdVBA_GetCellsContentToExcel

    'WORD 加载项 代码模板 Dim cmdBar As CommandBar, cmdBtn As CommandBarControl Const cmdBtnCap As String = &qu ...