1. 程式人生 > >POJ 1001 Exponentiation(大數冪,還是Java大發好!需呼叫多個方法)

POJ 1001 Exponentiation(大數冪,還是Java大發好!需呼叫多個方法)

Exponentiation
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 156303 Accepted: 38063

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

題意:輸入兩個數x,y,求x^y,(y為整型),結果小於0,輸出的結果去掉前面的"0.",直接輸出後面的. 如:stripTrailingZeros() toPlainString(); 呼叫的方法也差不多,但這題如果結果小於小於0,要去掉前導"0.",那該怎麼做呢? 把結果轉換成string型別,再呼叫java.lang.string類中的substring()方法就OK了. API中substring()方法的說明:
public String substring(int beginIndex) 返回一個新的字串,它是此字串的一個子字串。 該子字串從指定索引處的字元開始,
直到此字串末尾。
示例: 
 "unhappy".substring(2) returns "happy"
 "Harbison".substring(3) returns "bison"
 "emptiness".substring(9) returns "" (an empty string)
引數:
beginIndex - 起始索引(包括)。 
返回:
指定的子字串。 
丟擲: 
IndexOutOfBoundsException - 如果 beginIndex 為負或大於此 String 物件的長度。
substring
public String substring(int beginIndex,int endIndex)
返回一個新字串,它是此字串的一個子字串。 該子字串從指定的 beginIndex 處開始,直到索引 endIndex - 1 處的字元。 因此,該子字串的長度為 endIndex-beginIndex。 
示例: 
 "hamburger".substring(4, 8) returns "urge"
 "smiles".substring(1, 5) returns "mile"
引數:
beginIndex - 起始索引(包括)。
endIndex - 結束索引(不包括)。 
返回:
指定的子字串。 
丟擲: 
IndexOutOfBoundsException - 如果 beginIndex 為負,或 endIndex 大於此 String 物件的長度,
或 beginIndex 大於 endIndex。

AC程式碼:

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			BigDecimal a = sc.nextBigDecimal();
			int b = sc.nextInt();
			a = a.pow(b);
			String str = a.stripTrailingZeros().toPlainString();
			if (str.startsWith("0.")){
				str = str.substring(1);
			}
			System.out.println(str);
		}
	}

}


相關推薦

POJ 1001 Exponentiation(大數,還是Java!呼叫方法)

Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 156303 Accepted: 38063 Description Problems involving the

POJ 1001 Exponentiation

IT names columns class int systems sig hat min Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions:

POJ 1001: Exponentiation

rgs scanner set ati close system one strip new 題目描述 Problems involving the computation of exact values of very large magnitude and preci

學習Java資料就業嗎

大資料時代來臨,如此火爆的職業,吸引了大批有志青年的加入,再加入之前,你對大資料都瞭解嗎?他的行業前景如何?薪資水平如何? 1. 大資料行業分析 作為中國官方重點扶持的戰略性新興產業,大資料產業已逐步從概念走向落地“大資料”和“虛擬化”兩大熱門領域得到了廣泛關注和重視,90%企業都在實用大資

java牛需要學習的25要點

想成為Java大牛嗎?不妨來學習者25個要點。   1. 你需要精通面向物件分析與設計(OOA/OOD)、涉及模式(GOF,J2EEDP)以及綜合模式。你應該瞭解UML,尤其是class、object、interaction以及statediagrams。   2

java 一個文件中寫class

nds ride 中一 pan 文件名 out clas int [] 一個java文件中可以包含多個類,但這些類中一個特殊類,這個類的屬性為public,且這個類的名字與文件名相同,java文件中僅有一個類為public. interface Moveable

java -D參數簡化增加jar【簡化設置classpath】

同一文件 增加 main post ack nbsp 簡化 一個 -a 1、-D<name>=<value> set a system property 設置系統屬性。 java命令引入jar時能夠-cp參數,但時-cp不能用通配

Java中前臺往後臺傳id參數

ces 多個 ont div public nodelist ktr ice ret 1.傳字符串的形式,字符串中每個id用 “,” 隔開 打印出來如下形式: 前臺JS代碼 function saveRoleAdd() { v

java實現從命令行接收數字,求和之後輸出結果

system 程序流程圖 sta num 思想 pri for循環 含義 自動 1.設計思想 首先要了解從命令行輸入數字的含義,不需要在程序中自己定義。需要定義int類的num和sum。之後利用num=Integer.parseInt(arg);將String型轉化為int

IIS https綁定主機頭方法(可實現禁止直接通過IP訪問,好像可以實現IIS中綁定https<是使用單域名證書,非域名證書>)

多個 nbsp app for iis 直接 fig protoc eight 步驟: 打開:%systemroot%\SYSTEM32\inetsrv\config\ 打開:applicationHost.config 查找:<binding protocol="

java中介面是否可以繼承介面?

以下內容轉自:http://blog.csdn.net/tiantiandjava/article/details/46514783 介面是常量值和方法定義的集合。介面是一種特殊的抽象類。 java類是單繼承的。classB Extends classA java介面可以多繼承。Inte

Java中異常的捕獲順序(catch)

import java.io.IOException; public class ExceptionTryCatchTest { public void doSomething() throws IOException{ System.out.println("do

java的dao層如何返回list

比如一個場景 我要這樣展示(相同的元素只要展示一次) 前臺是這樣寫的 el表示式是不能對資料進行處理的,所以資料有重複的那麼就一定會顯示出來 所以,考慮,能不能直接在dao中查詢的時候直接返回三個list,這樣前臺直接取就行了 搜了一下,有類似需求   看起來是可以實現的,

java併發程式設計(二)執行緒

多個執行緒多個鎖 多個執行緒多個鎖:多個執行緒,每個執行緒都可以拿到自己制定的鎖,分別獲得鎖之後,執行synchronized方法體的內容。就是在上次那個部落格上說道的鎖競爭的問題,是因為所有的執行緒過來以後都爭搶同一個鎖。如果說每個執行緒都可以或得到自己的鎖,這樣的話我們的鎖競爭問題就沒有了

java 使用百度地圖顯示標註點帶彈出資訊框

直接上程式碼 <%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UT

一個".java"原始檔中是否可以包括類(不是內部類)?有什麼限制?

可以有多個類,但只能有一個public的類,可以是package或者abstract或者final的類,並且public的類名必須與檔名一致。 package Example; public cla

Java實現簡單的selenium開啟瀏覽器(Paramaters實現)

XML配置 <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="photo sui

【一道面試題】一個".java"原始檔中是否可以包括類(不是內部類)?有什麼限制?

這個面試題的答案在網上一搜一大把 但都是兩句話就結束了,我們來仔細看一下具體是什麼情況! 首先 肯定的一點是一個.java的原始檔中是可以包含多個類的,但是public類只能有一個,並且類名要和檔名相同,如果有兩個public類 就會報出以下的錯誤 這

java cmd命令列下加入jar包

cmd命令列模式下,我們要執行一個java類,一般的方法是: java -classpath xxx.jar Test 但是多數情況下我們的程式都是包含多個jar檔案的,怎麼樣在命令列下將這邊jar包都載入起來呢?如果一個一個的加是比較麻煩的,那麼有沒有一種方法直接指定下

玩一分鐘怎麽回血?回血有什麽方法?回血導師67263194

清醒 大發 process 保持 jpg 開始 環境 png 全部 人生,就像一條連續不斷的拋物線,有高峰,也有低谷,不斷的循環往復,直至我們生命的結束。我們每個人都希望自己一生都能夠處於順 境。於是我們對低谷抱有著抵觸的心理,對它的降臨很是抗拒。殊不知沒有低谷溝壑的人生是