pi |
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 47 Accepted Submission(s): 27 |
|
Problem Description
計算圓周率到小數點後5n 位。(本題不可打表)
|
Input
正整數n<300. n=0結束.
|
Output
圓周率pi到小數點後5n位.第1行輸出3.以5個數爲1段.每行最多10段,每段佔6列,右對齊.參看Sample
|
Sample Input
20 15 0 |
Sample Output
3. 14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 06286 20899 86280 34825 34211 70679 3. 14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 06286 |
好像是用什麼麥金公式,我也不知道,
http://blog.sina.com.cn/s/blog_a389f34a01013c11.htmlhtml
1 import java.math.BigDecimal; 2 import java.math.BigInteger; 3 import java.util.Scanner; 4 5 6 public class Main { 7 public static void main(String[] args) { 8 BigDecimal TWO=BigDecimal.valueOf(2); 9 BigDecimal ans=BigDecimal.valueOf(2); 10 for(int i=5000;i>=1;i--){ 11 ans=TWO.add(ans.multiply(td(i)).divide(td(2*i+1),1800,BigDecimal.ROUND_HALF_UP)); 12 } 13 String stra=ans.toString(); 14 Scanner sc=new Scanner(System.in); 15 while(sc.hasNext()){ 16 int n=sc.nextInt(); 17 if(n==0)break; 18 System.out.println("3."); 19 for(int i=0;i<n;i++){ 20 System.out.print(" "); 21 System.out.print(stra.substring(i*5+2,i*5+7)); 22 if((i+1)%10==0)System.out.println(); 23 } 24 if(n%10!=0)System.out.println(); 25 26 } 27 } 28 public static BigDecimal td(int n){ 29 return BigDecimal.valueOf(n); 30 } 31 }