HDU Exponentiation 1063 Java大數題解,hduexponentiation

來源:互聯網
上載者:User

HDU Exponentiation 1063 Java大數題解,hduexponentiation

ExponentiationTime 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]%......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.