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

 
 
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 R n 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 次方(R n),其中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++解题

  1   /*
  2   poj 1001
  3   version:1.0
  4   author:Knight
  5   Email:S.Knight.Work@gmail.com
  6   */
  7 
  8 #include<cstdio>
  9 #include<cstring>
 10 #include<cstdlib>
 11 #include<memory.h>
 12 using namespace std;
 13  
 14 char Result[200];//存R^N的结果
 15  
 16 //大实数的乘法,乘数为FirMultiplier和SecMultiplier,结果存在Result中
 17 void HigRealMul(char* FirMultiplier, char* SecMultiplier, char* Result);
 18 //剔除实数尾部的无效0或小数点
 19 void CutInsignificantTail(char* StrR);
 20 //计算小数点在实数中的位数
 21 int CountPointIndex(char* StrR);
 22 //删除实数中的小数点,PointIndex为小数点在实数中从右向左数的第几位
 23 void DeletePoint(char* StrR, int PointIndex);
 24  
 25 int main(void)
 26 {
 27     char StrR[10];//R对应的字符串
 28     int N;
 29     int i;
 30     int PointIndex = 0;//记录小数点在实数中从右向左数的第几位,如1.26在第3位,4在第0位
 31  
 32     while(scanf("%s%d", StrR, &N) != EOF)
 33     {
 34         memset(Result, 0, 200);
 35  
 36         CutInsignificantTail(StrR);
 37  
 38         PointIndex = CountPointIndex(StrR);
 39  
 40         DeletePoint(StrR, PointIndex);
 41  
 42         strcpy(Result, StrR);
 43  
 44         for (i=2; i<=N; i++)
 45         {
 46             HigRealMul(Result, StrR, Result);
 47         }
 48  
 49         int Len = strlen(Result);
 50  
 51         if (Len -(PointIndex - 1) * N < 0)
 52         {
 53             printf(".");
 54             for (i = Len - (PointIndex - 1) * N; i<0; i++)
 55             {
 56                 printf("0");
 57             }
 58         }
 59  
 60         for (i=0; i<Len; i++)
 61         {
 62             //输出小数点
 63             if (i == Len -(PointIndex - 1) * N)
 64             {
 65                 printf(".");
 66             }
 67             printf("%c", Result[i]);
 68         }
 69         printf("\n");
 70         //printf("%s\n", Result);
 71         //printf("%d\n", PointIndex);
 72     }
 73     return 0;
 74 }
 75  
 76 //大实数的乘法,乘数为FirMultiplier和SecMultiplier,结果存在Result中
 77 void HigRealMul(char* FirMultiplier, char* SecMultiplier, char* Result)
 78 {
 79  
 80     char TmpResult[200];
 81     int i,j;
 82     int k = -1;//控制TmpResult[]下标
 83     int FirLen = strlen(FirMultiplier);
 84     int SecLen = strlen(SecMultiplier);
 85  
 86     memset(TmpResult, '0', 200);
 87  
 88 //模拟乘法运算
 89     for (i=SecLen-1; i>=0; i--)
 90     {
 91         k++;
 92  
 93         int FirMul;
 94         int SecMul = SecMultiplier[i] - '0';
 95         int Carry;//进位
 96  
 97         for (j=FirLen-1; j>=0; j--)
 98         {
 99             FirMul = FirMultiplier[j] - '0';
100             TmpResult[k + FirLen - 1 - j] +=   FirMul * SecMul % 10;
101             Carry = FirMul * SecMul / 10 + (TmpResult[k + FirLen - 1 - j] - '0') / 10;
102             TmpResult[k + FirLen - 1 - j] = (TmpResult[k + FirLen - 1 - j] - '0') % 10 + '0';
103             TmpResult[k + FirLen - j] += Carry;
104         }
105     }
106  
107 //防止某一位的值超过9
108     for (k=0; k<200; k++)
109     {
110         TmpResult[k + 1] += (TmpResult[k] - '0') / 10;
111         TmpResult[k] = (TmpResult[k] - '0') % 10 + '0';
112     }
113 //将设置字符串结束符
114     for (k=199; k>=0; k--)
115     {
116         if ('0' != TmpResult[k - 1])
117         {
118             TmpResult[k] = '\0';
119             break;
120         }
121     }
122  
123 //将临时存储的答案TmpResult倒转变成我们熟悉的方式,存到Result中
124     for (i=strlen(TmpResult)-1,j=0; i>=0; i--,j++)
125     {
126         Result[j] = TmpResult[i];
127     }
128     Result[j] = '\0';
129  
130 }
131  
132 //剔除实数尾部的无效0或小数点
133 void CutInsignificantTail(char* StrR)
134 {
135     int i;
136     int PointIndex = CountPointIndex(StrR);
137     int Len = strlen(StrR);
138  
139     if (0 == PointIndex)
140     {
141         if ('.' == StrR[Len - 1])
142         {
143             StrR[Len - 1] = '\0';
144         }
145  
146         return;
147     }
148  
149     for (i=Len-1; i>Len-1-PointIndex; i--)
150     {
151         if ('0' == StrR[i] || '.' == StrR[i])
152         {
153             StrR[i] = '\0';
154         }
155         else
156         {
157             return ;
158         }
159     }
160 }
161  
162 //计算小数点在实数中的位数
163 int CountPointIndex(char* StrR)
164 {
165     int i;
166     int Index = 0;
167  
168     for (i = strlen(StrR); i>=0; i--)
169     {
170  
171         if ('.' == StrR[i])
172         {
173             break;
174         }
175         else
176         {
177             Index++;
178         }
179     }
180  
181     if (-1 == i)
182     {
183         Index = 0;
184     }
185  
186     return Index;
187  
188 }
189  
190 //删除实数中的小数点
191 void DeletePoint(char* StrR, int PointIndex)
192 {
193     int i;
194     int Len = strlen(StrR);
195  
196     for (i=strlen(StrR)-PointIndex; i<Len; i++)
197     {
198         StrR[i] = StrR[i+1];
199     }
200 }

 

JAVA解题:

 1 import java.io.*;
 2 import java.util.*;
 3 import java.math.*;
 4 
 5 public class Main1001 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         Scanner cinScanner = new Scanner(System.in);
12                 
13         while (cinScanner.hasNextBigDecimal()) {
14             BigDecimal r = cinScanner.nextBigDecimal();
15             int n = cinScanner.nextInt();
16             
17             BigDecimal answerBigDecimal = r.pow(n);
18             /*
19              * stripTrailingZero();去除小数后边的0,例如16.000000使用这个方法后,就变成了16
20              */
21             answerBigDecimal = answerBigDecimal.stripTrailingZeros();
22             
23             /*
24              * toPlainString();不使用科学计数法输出
25              */
26             String answerString = answerBigDecimal.toPlainString();
27             if (answerString.startsWith("0.")) {
28                 answerString = answerString.substring(1);
29             }
30             
31             
32             System.out.println(answerString);
33         }
34         
35 
36     }
37 
38 }

 

 

转载于:https://www.cnblogs.com/BTMaster/p/3524992.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值