1.链接地址:

http://poj.org/problem?id=1001

http://bailian.openjudge.cn/practice/2951

2.题目:

Exponentiation
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 127573   Accepted: 31149

Description

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.

This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input

The
input will consist of a set of pairs of values for R and n. The R value
will occupy columns 1 through 6, and the n value will be in columns 8
and 9.

Output

The
output will consist of one line for each line of input giving the exact
value of R^n. Leading zeros should be suppressed in the output.
Insignificant trailing zeros must not be printed. Don't print the
decimal point if the result is an integer.

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

Hint

If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer

C++

while(cin>>s>>n)

{

...

}

c

while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want

/*while(scanf(%s%d",s,&n)!=EOF) //this also work */

{

...

}

Source

3.思路:

高精度浮点数乘法,利用大整数乘法的基础计算

(1)判断是否为小数,不为小数则转为小数

(2)获取小数点位置,计算出相乘后的小数点的位置。并去除原小数的小数点

(3)利用大整数乘法求结果

(4)补全小数点,并补零

tip:注意一下例子

0.0000 1 结果为 0

1.0000 0 结果为 1

0.0000 0 结果为 1

并注意小于1的浮点数要去除前导的0

4.代码:

 #include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio> using namespace std; string mul(string str1,string str2)
{
vector<int> v_res(str1.size()+str2.size(),);
string::size_type i,j;
vector<int>::size_type k,p; reverse(str1.begin(),str1.end());
reverse(str2.begin(),str2.end());
for(i = ; i != str1.size(); ++i)
{
for(j = ; j != str2.size(); ++j)
{
v_res[i+j] += (str1[i]-'') * (str2[j] - '');
}
}
for(k = ; k != v_res.size() - ; ++k)
{
v_res[k+] += v_res[k] / ;
v_res[k] = v_res[k] % ;
} for(p = v_res.size() - ; p != -; --p)
{
if(v_res[p] != ) break;
}
if(p == -) p = ; string s_res(p+,'');
for(k = p; k != -; --k) s_res[p-k] = char(v_res[k] + ''); return s_res; } string real_mul(string str1,string str2)
{
string::size_type idx_str1_point = str1.find(".");
if(idx_str1_point == string::npos)
{
str1 += ".0";
idx_str1_point = str1.find(".");
}
str1.erase(idx_str1_point,); string::size_type idx_str2_point = str2.find(".");
if(idx_str2_point == string::npos)
{
str2 += ".0";
idx_str2_point = str2.find(".");
}
str2.erase(idx_str2_point,); string::size_type dec_res_len = (str1.size() - idx_str1_point) + (str2.size() - idx_str2_point); string res = mul(str1,str2); if(res.size() < dec_res_len + )
{
res = string(dec_res_len + - res.size(),'') + res;
} res.insert(res.size() - dec_res_len,"."); string::size_type idx_res_tail = res.find_last_not_of("");
res = res.substr(,idx_res_tail+); if(res[res.size() - ] == '.') res.erase(res.size()-); return res;
} int main()
{
string r;
int n;
while(cin>>r>>n)
{
string res = "";
while(n--)
{
res = real_mul(res,r);
}
string::size_type idx_res_pre = res.find_first_not_of("");
if(idx_res_pre != string::npos) res.erase(,idx_res_pre); cout<<res<<endl;
} return ;
}

Poj 1001 / OpenJudge 2951 Exponentiation的更多相关文章

  1. POJ 1001 Exponentiation(大数运算)

    POJ 1001 Exponentiation 时限:500 ms   内存限制:10000 K 提交材料共计: 179923   接受: 43369 描述:求得数R( 0.0 < R < ...

  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 : 大数乘法

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

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

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

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

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

  7. POJ 1001 Exponentiation

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

  8. POJ 1001 Exponentiation 模拟小数幂

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

  9. 【POJ 1001】Exponentiation (高精度乘法+快速幂)

    BUPT2017 wintertraining(15) #6A 题意 求\(R^n\) ( 0.0 < R < 99.999 )(0 < n <= 25) 题解 将R用字符串读 ...

随机推荐

  1. DRM你又赢了:其API纳入HTML5标准

    摘要:W3C今天发布了一份加密媒体扩展工作草案(EME),将支持DRM多媒体数字内容,而且浏览器将无需使用Flash或Silverlight.EME定义了一系列API,允许JavaScript和HTM ...

  2. iOS stretchableImageWithLeftCapWidth 图片放大不变形

    转载自:http://www.cnblogs.com/bandy/archive/2012/04/25/2469369.html - (UIImage *)stretchableImageWithLe ...

  3. android136 360 拖拽

    差补器原理: 图标拖拽:     activity_drag_view.xml <?xml version="1.0" encoding="utf-8"? ...

  4. Linux中的文件描述符与打开文件之间的关系------------每天进步一点点系列

    http://blog.csdn.net/cywosp/article/details/38965239 1. 概述     在Linux系统中一切皆可以看成是文件,文件又可分为:普通文件.目录文件. ...

  5. debian配置简单的vsftp服务器

    Ubuntu Linux与Windows系统不同,Ubuntu Linux不会产生无用垃圾文件,但是在升级缓存中,Ubuntu Linux不会自动删除这些文件,今天就来说说这些垃圾文件清理方法.  1 ...

  6. 实例源码--Android通讯录源码

      下载源码   技术要点: 1.通讯录联 系人的管理 2.接听.打电话 3.发短信 4. 源码带详细的 中文注释    ...... 详细介绍: 1.通讯录联系人的管理 播放器具有播放本地音乐的功能 ...

  7. python--列表的使用

    1.定义列表: names = ['Mo',"Tenglan",'Eric'] 通过下标访问列表中的元素,下标从0开始计数 >>> names[0] 'Mo' & ...

  8. Android(java)学习笔记132:ListViewProject案例(ListView + ArrayAdapter)

    1.首先是MainActivity.java文件,如下: package com.himi.lv1; import java.util.ArrayList; import java.util.List ...

  9. sass+require实现侧边栏

    一.效果图(如下)及使用的技术 实现用sass实现页面中右侧固定侧边栏的样式,用require.js实现返回顶部的功能 二.sass 具体的sass的介绍就不多说了,大家可以参考sass官网介绍,下面 ...

  10. Wireshark 过滤条件

    做应用识别这一块经常要对应用产生的数据流量进行分析. 抓包采用wireshark,提取特征时,要对session进行过滤,找到关键的stream,这里总结了wireshark过滤的基本语法,供自己以后 ...