HDU Exponentiation 1063 Java大數題解,hduexponentiation
Exponentiation
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6973 Accepted Submission(s): 1975
Problem DescriptionProblems 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.
InputThe 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.
OutputThe 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 120.4321 205.1234 156.7592 998.999 101.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201
大數的題目使用Java就變得其簡單無比。
主要就一句代碼:
String ans = a.pow(b).stripTrailingZeros().toPlainString();
去零和轉換為純字串輸出。
這使用C++要數十行代碼。
對比C++解法:http://blog.csdn.net/kenden23/article/details/23997827
相比之下,java就成了水題了,學不到什麼東西。
import java.math.BigDecimal;import java.util.*;public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNext()) { BigDecimal a = scan.nextBigDecimal(); int b = scan.nextInt(); String ans = a.pow(b).stripTrailingZeros().toPlainString(); if (ans.startsWith("0")) ans = ans.substring(1); System.out.println(ans); } scan.close(); }}
北大ACM1001 exponentiation 用C語言怎寫?對高精度數應該怎處理?
昨天剛做的這個題,現在把大概思路和代碼(c++)給你吧
這個題很明顯不能直接乘,因為沒有那個類型能到達如此之精度,因為有好多次方嗎,於是我把每次相乘的結果結果儲存在int r[151]數組裡面,數組中每個元素儲存結果中的一位,如例子中最後一組1.01 的12次方的結果為1.126825030131969720661201,那麼最後,r中元素為:r[1]=1、r[2]=1、r[3]=2、r[4]=6........
計算時乘數忽略了小數點和無效0,如1.0100計算時使用101,而r初始值為乘數即,r[1]=1,r[2]=0、r[3]=1;然後與乘數101相乘12-1=11次,每次相乘都是r中每個元素與乘數相乘,相乘後r中每個元素不在是儲存一位元,於是需要進位,使r中元素仍只有一位元。這樣計算的話,r數組中每個元素都不會超出精度,因為每次相乘都是一位元和乘數相乘。由於忽略了小數點,所以計算前需要確定是否有小數點,如果有的話小數點後有幾位有效數字也要確定,然後就可以得出計算完成後小數點後有幾位有效數字,輸出就能將小數點加進去了。
這就是大概過程 代碼如下:
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string mlp; //乘數
int power; //乘數的冪
int r[151]; //儲存結果
int hdot;
while(cin>>mlp>>power)
{
hdot=0;
for(int t=0;t<150;t++)
{
r[t]=-1;
}
if(mlp.find(".")!=string::npos)
hdot=mlp.length()-mlp.find(".")-1;
string::iterator itr=mlp.end()-1;
while(hdot>0&&itr>=mlp.begin())
{
if(*itr!='0')
{break;}
hdot--;
itr--;
}
int cn=0;
while(itr>=mlp.begin())
{
if(*itr!='.')
{
r[cn]=*itr-'0';
cn++;
}
itr--;
}
int k=cn-1;
int m=0; //儲存臨時數;
while(k>-1)
{
m=m*10+r[k];
k--;
}
for(int i=1;i<power;i++)
{
int j=0;
while(r[j]>-1)
{
r[j]=r[j]*m;
j++;
}
j=0;
while(r[j]>-1)
{
if(r[j+1]==-1&&r[j]>=10)
r[j+1]=r[j]/10;
else
r[j+1]+=r[j]/10;
r[j]=r[j]%......餘下全文>>