Java在大數的處理上比C確實流氓多了,在這裏我整理一些有關Java的一些基礎知識和模板,以便以後學習。html
內容大多摘自兩位師哥,這裏給出原文的連接java
---愛國師哥https://www.cnblogs.com/aiguona/p/9133554.htmlc++
--福星師哥https://blog.csdn.net/akatsuki__itachi/article/details/81152232數組
第一步:file->new->java project->起名->finisheclipse
第二步:進入到剛纔建的工程裏,右鍵src->new->package->起名->finishide
第三步:進入到剛纔建的package裏,右鍵name->new->class->起名(這裏起名要注意,由於比賽時若是交java代碼,這裏的類名就要命名爲Main,區分大小寫)函數
Java的輸入是先定義一個scanner,而後用這個進行輸入,而且每一種輸入都有相應的輸入函數,具體以下:學習
1 public class Main 2 { 3 public static void main(String[] args) 4 { 5 Scanner cin = new Scanner ( System.in); 6 int a; 7 double b; 8 BigInteger c; 9 String d; 10 a = cin.nextInt(); 11 b = cin.nextDouble(); 12 c = cin.nextBigInteger(); 13 d = cin.nextLine(); 14 // 每種類型都有相應的輸入函數. 15 } 16 }
Java的輸出spa
1 System.out.println(a + " " + b);//換行 2 System.out.printf("%d %10.5f\n", a, b); 3 4 /*規格化的輸出: 5 函數:這裏0指一位數字,#指除0之外的數字(若是是0,則不顯示),四捨五入.*/ 6 DecimalFormat fd = new DecimalFormat("#.00#"); 7 DecimalFormat gd = new DecimalFormat("0.000"); 8 System.out.println("x =" + fd.format(x)); 9 System.out.println("x =" + gd.format(x));
這裏給出幾個簡單代碼.net
1.輸出hellow world
1 package BigInteger; 2 public class Main { 3 public static void main(String args[]) { 4 System.out.println("Hello world"); 5 } 6 }
關於上面的package BigInteger,提交代碼時不須要粘貼。
2.計算a+b
1 package BigInteger; 2 import java.util.*;//導包,至關於c/c++裏的頭文件 3 public class Main { 4 public static void main(String args[]) { 5 Scanner cin = new Scanner(System.in); 6 int a,b; 7 a = cin.nextInt(); 8 b=cin.nextInt(); 9 int ans = a + b; 10 System.out.println(ans); 11 } 12 }
3.多組輸入輸出
1 package BigInteger; 2 import java.util.*; 3 public class Main { 4 public static void main(String args[]) { 5 Scanner cin = new Scanner(System.in); 6 int a, b; 7 while (cin.hasNext()) { 8 a = cin.nextInt(); 9 b = cin.nextInt(); 10 int ans = a + b; 11 System.out.println(ans); 12 } 13 } 14 }
下面就開始說大數的相關操做
首先咱們須要導包,即BigIntegr類 和 BigDecimal類所在的包
import java,math.*;
*就表明導入包math裏面全部的類,若是你不喜歡看到 *
那麼你也能夠寫 import java,math.BigInteger; import java,math.BigDecimal;
1.大整數的加減乘除求餘等計算
1 /* 2 大數的加減運算不一樣於普通整數的加減乘除運算 3 加—— a+b: a=a.add(b); 4 減—— a-b: a=a.subtract(b); 5 乘—— a*b: a=a.multiply(b); 6 除—— a/b: a=a.divide(b); 7 求餘—a%b: a=a.mod(b); 8 轉換—a=b: b=BigInteger.valueOf(a); 9 比較 if (ans.compareTo(x) == 0)//比較 10 System.out.println("相等"); 11 System.out.println("a + b = "+ans_add); // 這裏的‘+’ (第二個) 是鏈接的意思 12 */ 13 package wkf; 14 import java.util.*; 15 import java.math.*; 16 public class Main { 17 public static void main(String args[]) { 18 Scanner cin = new Scanner(System.in); 19 BigInteger a,b,x,y; 20 BigInteger ans_add,ans_sub,ans_mul,ans_div,ans_mod; 21 a=cin.nextBigInteger(); 22 b=cin.nextBigInteger(); 23 ans_add = a.add(b); //a+b 24 ans_sub = a.subtract(b); //a-b 25 ans_mul = a.multiply(b); //a*b 26 ans_div = a.divide(b); //a/b 27 ans_mod = a.mod(b); //a%b 28 x=BigInteger.valueOf(1);//轉換 29 System.out.println("a + b = "+ans_add); 30 System.out.println("a - b = "+ans_sub); 31 System.out.println("a * b = "+ans_mul); 32 System.out.println("a / b = "+ans_div); 33 System.out.println("a % b = " + ans_mod); 34 System.out.println(x); 35 if (a.compareTo(b) == 0)//比較 36 System.out.println("相等"); 37 else 38 System.out.println("不相等"); 39 } 40 }
UVA—10106:a*b
1 import java.math.BigInteger; 2 import java.util.*; 3 public class Main { 4 public static void main(String args[]) { 5 Scanner s = new Scanner(System.in); 6 while(s.hasNext()) { 7 BigInteger a=s.nextBigInteger(); 8 BigInteger b=s.nextBigInteger(); 9 BigInteger ans=a.multiply(b); 10 System.out.println(ans); 11 } 12 } 13 }
UVA—424:多個數連續相加,遇到0就中止相加,給出結果
1 import java.math.*; 2 import java.util.Scanner; 3 import java.io.*; 4 public class Main { 5 public static void main(String args[]){ 6 BigInteger ans = BigInteger.valueOf(0); 7 BigInteger x; 8 BigInteger y = BigInteger.valueOf(0); 9 Scanner cin = new Scanner(System.in); 10 while(cin.hasNext()){ 11 x = cin.nextBigInteger(); 12 if(x.equals(y)) ///加到0就要中止相加 13 break; 14 ans = ans.add(x); 15 } 16 System.out.println(ans); 17 } 18 }
HDU - 1042:計算階乘:
1 package wkf; 2 import java.io.*; 3 import java.math.BigInteger; 4 import java.util.*; 5 public class Main// 注意在oj提交是要用Main 6 { 7 public static void main(String[] args) { 8 Scanner in = new Scanner(System.in); 9 int n; 10 while (in.hasNext()) { 11 n = in.nextInt(); 12 BigInteger sum = BigInteger.valueOf(1); 13 for (int i = 1; i <= n; i++) 14 sum = sum.multiply(BigInteger.valueOf(i)); 15 System.out.println(sum); 16 } 17 } 18 }
UVA —10494
求兩個大數相除或者求餘
import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); BigInteger a, b, t = new BigInteger("1"); String c; while (s.hasNext()) { a = s.nextBigInteger(); c = s.next(); b = s.nextBigInteger(); if (c.equals("%"))///equals用來比較的是兩個對象的內容是否相等 t = a.mod(b); if (c.equals("/")) t = a.divide(b); System.out.println(t); } } }
POJ1001 計算a^b 注意是小數
1 import java.util.*; 2 import java.math.*; 3 4 public class Main { 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner input = new Scanner(System.in); 8 while (input.hasNext()) { 9 BigDecimal a = input.nextBigDecimal(); // 大數類的double; 10 int b = input.nextInt(); 11 a = a.pow(b); 12 String ans = a.stripTrailingZeros().toPlainString(); // 去掉尾部零,轉換成非科學計數法字符串 13 if (ans.charAt(0) == '0') { // 若是以0開頭 14 ans = ans.substring(1); // 返回以位置1開頭的該字符串 15 } 16 System.out.println(ans); 17 } 18 } 19 }
關於BigDecimal的用法大體上和BigInteger同樣。
不過這裏須要提一下,在進行大浮點數運算的時候,小數點後面可能會含有多餘的後導0
好比0.5000,在題目要求中可能只須要輸出0.5
固然,有的題目可能還會要求小數點前面的0也要去掉,輸入.5
這時候咱們就須要去除掉後導0
轉化成 字符型的
方法以下:
1 String str; 2 str=ans.stripTrailingZeros().toPlainString();//去除全部後導0,而且轉化成字符型 3 //ans爲大浮點數運算後獲得的答案 4 //若是小數點前面的0也須要去掉,那麼輸出的時候處理一下便可: 5 if(str.charAt(0)=='0')//若是以0開頭 6 System.out.println(str.substring(1));//返回以位置1開頭的字符串 7 else 8 System.out.println(str);
HDU—1023:計算卡特蘭數
1 //卡特蘭數遞推公式h(n)=h(n-1)*(4*n-2)/(n+1); 2 import java.math.*; 3 import java.util.*; 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 BigInteger dp[];//定義一個數組 8 dp=new BigInteger[110];//規定數組的大小 9 dp[1]=BigInteger.valueOf(1); 10 int i,m; 11 for(i=2;i<=100;i++)//卡特蘭數打表 12 { 13 dp[i]=dp[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1)); 14 } 15 while(cin.hasNext()) 16 { 17 m=cin.nextInt(); 18 System.out.println(dp[m]); 19 } 20 } 21 }