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

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

大意:

对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。

现在要你解决的问题是:对一个实数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 。如果输出是整数,不要输出小数点。

解题思路:

(1)pow精度不够

函数原型:double pow( double x, double y );

头文件:math.h/cmath(C++中)

功能:计算x的y次方

返回值:x不能为负数且y为小数,或者x为0且y小于等于0,返回幂指数的结果。

 #include<math.h>
#include<stdio.h>
int main()
{
double x=2.0,y=3.0;
printf("%lf raised to %lf is %lf\n",x,y,pow(x,y));
return ;
}

(2)模拟乘法运算过程

  必须自己模拟乘法运算过程,进行高精度的运算。

  题目把输入的数限制在了6位,即只有5位数字,方便了我们解题。

  我们将输入数字,以字符串形式读入,找到小数所在的位数,计算出结果应包含的小数的位数,比如说第一个算式“95.123 12”的小数的位数应该有3*12位。

  解题的关键在于进行数据的处理,即模拟整数乘法,使用倒序数组,为什么使用倒序数组呢?我们模拟一下竖式乘法过程,例如:123*123:

  1  2  3

 x         3

——————

  3  6  9

先用个位乘被乘数;

  1  2  3

 x     2

——————

  2  4  6

  +    3  6   9

——————

  2  7  12  9

在用十位乘被乘数,并相加。

对大于10的进行进位:2  7  12  9 ==》 2  8  2  9

  1  2  3

 x  1

——————

  1  2  3

 +     2  8  2  9

————————

  1  4  11 2  9

在用百位乘被乘数,并相加。

对大于10的进行进位:1  4  11 2  9 ==》 1  5  1  2  9

  从上面的乘法竖式中,我们是向前进位,由于我们不知道最后得到的数据的位数,所以采用倒序存储,向后进位;从0位开始乘,结果也从0位开始存,非常方便。这样得出的结果也是倒序的。

  为了计算方便,我们不把数字多余的0去掉,而是在最后判断小数点的位置,然后输出该输出的数位。

  每次乘法,都是用题目给的r乘上你上一次得出的结果,如果是第一次计算,那么就是r,计算的结果直接加到一个初始化为0的250大小的数组里面,注意是加。乘法的过程很容易模拟出来,就是让乘数让乘数第一位把你的结果的所有位乘一遍,然后再让第二位乘,注意加进数组时,有一个偏移量,乘法竖式都是这么写的,大家体会下。

  开一个250的数组m存储结果,并且开一个250的数组jieguo存储中间结果,并且需要一个长度为6的int型数组存储输入的String类型的数据r。

  输出结果时要小心,因为数组中存储的数位是倒序的,且没有小数点,需要计算从两边向中间的小数点方向第一个不为0数的位置。

 #include<iostream>
#include<cstring>
#include<string>
#define MAX 250
using namespace std;
int main()
{
string r;//底数
int n,dian;
short Chengshu[];
short m[MAX],jieguo[MAX];
while(cin>>r>>n)
{
for(int i=;i<=MAX;i++) {m[i]=jieguo[i]=;}//初始化
for(int j=;j<;j++) {Chengshu[j]=;}
dian = ;//小数点位置
size_t pos = r.find(".");
if(pos != string::npos) dian = (-pos)*n;
for(int i=,j=;i>=;i--)
{//注意此处进行倒序存储
if(r[i] != '.') {
Chengshu[j] = m[j] = jieguo[j] = r[i]-'';
//此处要给m[]赋值,当n=1时直接输出
j++;
}
}
while(n>=)//当乘幂大于等于2的时候,等于1可直接处理0输出
{
for(int i=;i<MAX;++i) m[i]=;
for(int i=;i<;i++)
{//chengshu[i]
for(int j=;j<MAX;j++)
{
if(Chengshu[i]==) break;
else{
m[i+j] += Chengshu[i]*jieguo[j];
//对>9 的进行处理
for(int t=i+j;m[t]>;++t)//处理进位
{
m[t+]+=m[t]/;
m[t]=m[t]%;
}
}
}
}
//更新被乘数
for(int k=;k<MAX;k++)
{
jieguo[k] = m[k];
}
n--;
}
int first = -;
for(int i=MAX-;i>=dian;i--)
{//从右向左查找第一个不为0的位
if(m[i]>)
{
first = i;break;
}
}
int last = dian;
for(int j=;j<dian;j++)
{//从左向右查找小数点后第一个不为0的位
if(m[j]>) {
last = j;break;
}
} if(first != -)//整数位不全为0
{
for(int k=first;k>=dian;k--)
cout<<m[k];
} if(last != dian)//有小数位
{
cout<<".";
for(int k=dian-;k>=last;k--)
cout<<m[k];
}
cout<<endl;
}
return ;
}

java中有一个类 :java.math.BigDecimal

 import java.io.*;
import java.util.*;
import java.math.BigDecimal; public class Main
{
public static void main(String args[])throws Exception
{
Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{
BigDecimal r=cin.nextBigDecimal();
int n=cin.nextInt();
r=r.pow(n).stripTrailingZeros();//去掉小数点后面的零
String m_string=r.toPlainString();//不带指数段的字符串表示形式
if(m_string.charAt(0)=='0')
m_string=m_string.substring(1);
System.out.println(m_string);
}
}
}
 
 
 

Exponentiation(求高精度幂)的更多相关文章

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

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

  2. 求高精度幂(java)

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

  3. 【ACM】求高精度幂

    题目来源:http://poj.org/problem?id=1001&lang=zh-CN 求高精度幂 Time Limit: 500MS   Memory Limit: 10000K To ...

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

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

  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. 【PKU1001】Exponentiation(高精度乘法)

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 145642   Accepted: 35529 ...

  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. 使用java求高精度除法,要求保留N位小数

    题目要求是高精度除法,要求保留N位小数(四舍五入),并且当整数部分为0时去除0的显示 import java.math.BigDecimal; import java.util.Scanner; pu ...

随机推荐

  1. django form 和modelform样式设置

      目录 1.form通过attr设置属性 2.输入框设置表单状态 3.modelform的使用 4.结合modelform 使用for循环生成输入框 5.基于init构造方法设置样式 6.基本增删改 ...

  2. java Spring boot项目简单说明

    前言 一直从事.NET开发,但一直有种想去探索Java世界的冲动,今天终于有时间来尝试一下,以下是自己探索过程的简要记录. 一.开发工具 开发工具选用 IntelliJ IDEA社区版(免费),安装教 ...

  3. 如何对Linux内核参数进行优化?

    打开配置文件 vi /etc/sysctl.conf 输入配置,如下是内核优化的参数 # TCP三次握手建立阶段接收SYN请求队列的最大长度,默认为1024(将其设置得大一些可以使出现Nginx繁忙来 ...

  4. 5.SpringMVC 配置式开发-处理器适配器

    处理器适配器HandlerAdapter 1.SimpleControllerHandlerAdapter(默认) 所有实现了 Controller 接口的处理器 Bean,均是通过SimpleCon ...

  5. pycharm中代码窗口如何分成左右或者上下双栏

    操作步骤如下: 其中window->edit_tabs->Split Vertically 是分成左右双栏:选择Split Horizontally 是分成上下双栏

  6. OpenCV入门学习资料汇总

    OpenCV学习文档资料 OpenCV学习:1)OpenCV中文网站——http://wiki.opencv.org.cn/index.php/%E9%A6%96%E9%A1%B5 2)python实 ...

  7. Windows7用VirtualBox虚拟Ubuntu共享文件夹的终极方式

    在Win7用VirtualBox虚拟机安装Ubuntu后,共享文件夹再也不用手工mount了 安装增强工具包 设置共享文件夹后 VB已经自动挂载Windows文件夹到 /media/sf_*** 目录 ...

  8. Mysql(五):索引原理与慢查询优化

    一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...

  9. Hadoop_27_MapReduce_运营商原始日志增强(自定义OutputFormat)

    1.需求: 现有一些原始日志需要做增强解析处理,流程: 1. 从原始日志文件中读取数据(日志文件:https://pan.baidu.com/s/12hbDvP7jMu9yE-oLZXvM_g) 2. ...

  10. Java笔记(第六篇-网络通信)

    TCP/IP模式是一种层次结构,共分为四层,分别为应用层.传输层.互联网层和主机到网络层. 在TCP/IP协议栈中,有两个高级协议是网络应用程序编写者应该了解的,即“传输控制协议”(TCP)与“用户数 ...