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

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 */

{

...

} 翻译:
求高精度幂
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 126980   Accepted: 30980

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 。如果输出是整数,不要输出小数点。
 

解决思路

这是一道高精度的题,主要是处理前导0和末尾0的时候有点麻烦。例如100.00可能会处理成1。

源码

C++解题

   /*
poj 1001
version:1.0
author:Knight
Email:S.Knight.Work@gmail.com
*/ #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<memory.h>
using namespace std; char Result[];//存R^N的结果 //大实数的乘法,乘数为FirMultiplier和SecMultiplier,结果存在Result中
void HigRealMul(char* FirMultiplier, char* SecMultiplier, char* Result);
//剔除实数尾部的无效0或小数点
void CutInsignificantTail(char* StrR);
//计算小数点在实数中的位数
int CountPointIndex(char* StrR);
//删除实数中的小数点,PointIndex为小数点在实数中从右向左数的第几位
void DeletePoint(char* StrR, int PointIndex); int main(void)
{
char StrR[];//R对应的字符串
int N;
int i;
int PointIndex = ;//记录小数点在实数中从右向左数的第几位,如1.26在第3位,4在第0位 while(scanf("%s%d", StrR, &N) != EOF)
{
memset(Result, , ); CutInsignificantTail(StrR); PointIndex = CountPointIndex(StrR); DeletePoint(StrR, PointIndex); strcpy(Result, StrR); for (i=; i<=N; i++)
{
HigRealMul(Result, StrR, Result);
} int Len = strlen(Result); if (Len -(PointIndex - ) * N < )
{
printf(".");
for (i = Len - (PointIndex - ) * N; i<; i++)
{
printf("");
}
} for (i=; i<Len; i++)
{
//输出小数点
if (i == Len -(PointIndex - ) * N)
{
printf(".");
}
printf("%c", Result[i]);
}
printf("\n");
//printf("%s\n", Result);
//printf("%d\n", PointIndex);
}
return ;
} //大实数的乘法,乘数为FirMultiplier和SecMultiplier,结果存在Result中
void HigRealMul(char* FirMultiplier, char* SecMultiplier, char* Result)
{ char TmpResult[];
int i,j;
int k = -;//控制TmpResult[]下标
int FirLen = strlen(FirMultiplier);
int SecLen = strlen(SecMultiplier); memset(TmpResult, '', ); //模拟乘法运算
for (i=SecLen-; i>=; i--)
{
k++; int FirMul;
int SecMul = SecMultiplier[i] - '';
int Carry;//进位 for (j=FirLen-; j>=; j--)
{
FirMul = FirMultiplier[j] - '';
TmpResult[k + FirLen - - j] += FirMul * SecMul % ;
Carry = FirMul * SecMul / + (TmpResult[k + FirLen - - j] - '') / ;
TmpResult[k + FirLen - - j] = (TmpResult[k + FirLen - - j] - '') % + '';
TmpResult[k + FirLen - j] += Carry;
}
} //防止某一位的值超过9
for (k=; k<; k++)
{
TmpResult[k + ] += (TmpResult[k] - '') / ;
TmpResult[k] = (TmpResult[k] - '') % + '';
}
//将设置字符串结束符
for (k=; k>=; k--)
{
if ('' != TmpResult[k - ])
{
TmpResult[k] = '\0';
break;
}
} //将临时存储的答案TmpResult倒转变成我们熟悉的方式,存到Result中
for (i=strlen(TmpResult)-,j=; i>=; i--,j++)
{
Result[j] = TmpResult[i];
}
Result[j] = '\0'; } //剔除实数尾部的无效0或小数点
void CutInsignificantTail(char* StrR)
{
int i;
int PointIndex = CountPointIndex(StrR);
int Len = strlen(StrR); if ( == PointIndex)
{
if ('.' == StrR[Len - ])
{
StrR[Len - ] = '\0';
} return;
} for (i=Len-; i>Len--PointIndex; i--)
{
if ('' == StrR[i] || '.' == StrR[i])
{
StrR[i] = '\0';
}
else
{
return ;
}
}
} //计算小数点在实数中的位数
int CountPointIndex(char* StrR)
{
int i;
int Index = ; for (i = strlen(StrR); i>=; i--)
{ if ('.' == StrR[i])
{
break;
}
else
{
Index++;
}
} if (- == i)
{
Index = ;
} return Index; } //删除实数中的小数点
void DeletePoint(char* StrR, int PointIndex)
{
int i;
int Len = strlen(StrR); for (i=strlen(StrR)-PointIndex; i<Len; i++)
{
StrR[i] = StrR[i+];
}
}

JAVA解题:

 import java.io.*;
import java.util.*;
import java.math.*; public class Main1001 { /**
* @param args
*/
public static void main(String[] args) {
Scanner cinScanner = new Scanner(System.in); while (cinScanner.hasNextBigDecimal()) {
BigDecimal r = cinScanner.nextBigDecimal();
int n = cinScanner.nextInt(); BigDecimal answerBigDecimal = r.pow(n);
/*
* stripTrailingZero();去除小数后边的0,例如16.000000使用这个方法后,就变成了16
*/
answerBigDecimal = answerBigDecimal.stripTrailingZeros(); /*
* toPlainString();不使用科学计数法输出
*/
String answerString = answerBigDecimal.toPlainString();
if (answerString.startsWith("0.")) {
answerString = answerString.substring(1);
} System.out.println(answerString);
} } }
 

[POJ 1001] Exponentiation C++解题报告 JAVA解题报告的更多相关文章

  1. POJ 1001 Exponentiation(大数运算)

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

  2. [POJ 1000] A+B Problem 经典水题 C++解题报告 JAVA解题报告

        A+B Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 311263   Accepted: 1713 ...

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

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

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

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

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

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

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

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

  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. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

随机推荐

  1. linux文件系统和目录树的关系

    文件系统是和底层的硬件系统紧密关联的,文件系统相当于是dev(设备或硬件)在Linux上面的显示,如/dev/hdc2 而目录树是逻辑的概念,其可以通过挂载的方式连接文件系统,先用df查看本地的文件系 ...

  2. linux-2.6.22.6/Makefile:416: *** mixed implicit and normal rules: deprecated syntax

    今天在按照韦东山大哥的教程流程编译内核的时候出现了这个问题      linux-2.6.22.6/Makefile:416: *** mixed implicit and normal rules: ...

  3. elasticsearch dump加过滤条件(--searchBody)出错的解决 Unexpected token ' in JSON at position 0

    环境:本文测试在es2.4,win10下进行 es dump导数据可以加过滤条件,只导满足条件的数据.方法是用—searchBody参数,值是查询时的查询条件的json格式,例如 然而按官网和网上的格 ...

  4. Nginx 基本配置介绍

    一.什么是Nginx Nginx 是一个免费的,开源的,高性能的HTTP服务器和反向代理,以及IMAP / POP3代理服务器. Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资源消耗而闻 ...

  5. python+selenium之验证码的处理

    对于web应用来说,大部分的系统在用户登录时都要求用户输入验证码.验证码的类型很多,有字母数字的,有汉字的.甚至还有需要用户输入一道算术题的答案的.对于系统来说,使用验证码可以有效地防止采用机器猜测方 ...

  6. 【Python图像特征的音乐序列生成】数据集制作的一些tricks

    关于数据集的制作,我决定去掉很多不必要的东西,比如和弦,于是我选择了melody部分的旋律. 有了midi文件,我现在要abc序列,所以我要通过midi2abc转换一下文件. 批处理程序效果如下: 文 ...

  7. ABAP vs Java, 蛙泳 vs 自由泳

    去年7月定下的一年之内学会自由泳的目标终于实现了,特来还愿. ABAP和Java, 蛙泳和自由泳.前面的组合是Jerry用来挣钱养家的技术,后者是Jerry花了大量业余时间和金钱苦练的技能.或许有的朋 ...

  8. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] A A Problem about Polyline(数学)

    题目中给出的函数具有周期性,总可以移动到第一个周期内,当然,a<b则无解. 假设移动后在上升的那段,则有a-2*x*n=b,注意限制条件x≥b,n是整数,则n≤(a-b)/(2*b).满足条件的 ...

  9. Java 原型模式(克隆模式)

      Java 的设计模式有 23 种,前段时间小编已经介绍了单例模式,由于我们在学习 Spring 的时候在 bean 标签的学习中碰到了今天要讲的原型模式,那么小编就已本文来介绍下原型模式. 原型模 ...

  10. SC || Chapter 3

    ┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉ 基本数据类型 && 对象数据类型 基本数据类型(int char long) 在栈中分配内存,不可变 对象数据类型(String BigInt ...