为了熟悉java , 开一套poj大数处理的题来写.
-------------------------------------------------------------------
A:
(1)主要时间花在在处理输入上:
System.setIn(new FileInputStream("test.txt"));
相当于C++ 里面的 freopen("test.txt","r",stdin);
System.setOut(new FileOutputStream("ans.out"));
相当于C++里面的 freopen("ans.out","w",stdout);
它应该要加在 Scannercin = new Scanner (System.in); 的前面;
(2)cin.hashNext() 用来判断读入结束;
文件输入输出:
BufferedReader rd = new BufferedReader(new FileReader("lottery.in"));
PrintWriter ot = new PrintWriter("lottery.out");
Scanner in = new Scanner(rd);
ot.println(ansBigInteger);
rd.close();
ot.close();
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
import java.io.*; import java.math.*; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException { // System.setIn(new FileInputStream("test.txt")); Scanner cin = new Scanner (System.in); String str; BigDecimal ans,cur,eight; while (cin.hasNext()) { eight = BigDecimal.valueOf(8); ans = BigDecimal.valueOf(0); cur = BigDecimal.valueOf(1); str = cin.nextLine(); int pos = str.indexOf("."); BigDecimal tmp; for (int i = pos+1 ; i < str.length(); i++) { cur = cur.divide(eight); tmp = BigDecimal.valueOf(str.charAt(i)-'0'); tmp = tmp.multiply(cur); ans = ans.add(tmp); } System.out.println(str+" [8]"+" = "+ans+" [10]"); } System.exit(0); } }
B:
(1)void main要设成 static , 于是所有要调用的函数都要用static修饰;
(2)BigIntegerans ans = BigInteger.valueOf(0); //可以得到一个值为int的 大数 ans
(3)大数ans的加/减/乘/除/都可以通过调用 ans.add(t) / add.subtract(t) / ans.multiply(t) /ans.divide(t) ; 参数t 必须也是大数;
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.math.BigInteger; 4 import java.util.Arrays; 5 import java.util.Scanner; 6 7 8 public class Main 9 { 10 static private int maxn = 110; 11 static BigInteger dp[] = new BigInteger[maxn]; 12 static BigInteger unvis = BigInteger.valueOf(-1); 13 14 static private BigInteger getVal(int n) 15 { 16 BigInteger ans; 17 ans = BigInteger.valueOf(n); 18 /* 19 for (int i=1 ; i<=n ; i++ ) 20 ans = ans.multiply( BigInteger.valueOf(2) ); 21 ans = ans.subtract(BigInteger.valueOf(1));*/ 22 return ans; 23 } 24 static private BigInteger solv(int n) 25 { 26 BigInteger ans = BigInteger.valueOf(0); 27 BigInteger tmp; 28 int i; 29 if (dp[n] != unvis) return dp[n]; 30 // System.out.println("n="+n); 31 for ( i=1 ; i<=n ; i++ ) 32 { 33 tmp = getVal(i); 34 tmp = tmp.multiply(solv(n-i)); 35 // System.out.println("i="+i+" tmp="+tmp); 36 ans = ans.add(tmp); 37 } 38 return dp[n] = ans; 39 } 40 41 static public void main(String[] args) throws FileNotFoundException 42 { 43 Scanner cin = new Scanner(System.in); 44 int n; 45 while (cin.hasNext()) 46 { 47 n = cin.nextInt(); 48 Arrays.fill(dp, unvis); 49 dp[0] = BigInteger.valueOf(1); 50 System.out.println(solv(n)); 51 } 52 System.exit(0); 53 } 54 55 }
C:
(1)大数的进制转换:
BigInter m = new BigInteger(st, base); // st以base进制转换成10进制
类似的进制转换还有:
String str = Integer.toString(num, base); // 把num当做10进制的数转成base进制的str(base <= 35).
int num = Integer.parseInt(st, base); // 把st当做base进制,转成10进制的int
(2)关于字符串的操作:
java的string是不可变类型,需要进行各种修改赋值操作的话,还是用stringBuffer比较方便
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.math.BigInteger; 4 import java.util.Scanner; 5 6 7 public class Main 8 { 9 static BigInteger trans[] = new BigInteger[1000]; 10 static char retrans[] = new char[1000]; 11 static String TransToString(BigInteger ans,BigInteger base) 12 { 13 StringBuffer res = new StringBuffer(); 14 BigInteger zero = BigInteger.valueOf(0); 15 if (ans.compareTo(zero) == 0) 16 { 17 res = res.append(0); 18 return res.toString(); 19 } 20 while (ans.compareTo(zero) > 0) 21 { 22 int tmp = ans.mod(base).intValue(); 23 res = res.append(retrans[tmp]); 24 ans = ans.divide(base); 25 } 26 res = res.reverse(); 27 return res.toString(); 28 } 29 static String getAns(String str,BigInteger base1,BigInteger base2) 30 { 31 int len = str.length(),i; 32 char[] ch = str.toCharArray(); 33 BigInteger cur = BigInteger.valueOf(1); 34 BigInteger ans = BigInteger.valueOf(0); 35 for ( i=len-1 ; i>=1 ; i-- ) 36 { 37 BigInteger tmp = trans[ch[i]].multiply(cur); 38 cur = cur.multiply(base1); 39 ans = ans.add(tmp); 40 } 41 String resString = TransToString(ans,base2); 42 return resString; 43 } 44 static void initTrans() 45 { 46 int idx=0; 47 char i; 48 for ( i='0' ; i<='9' ; i++ ) {trans[i] = BigInteger.valueOf(idx++); retrans[idx-1]=i;} 49 for ( i='A' ; i<='Z' ; i++ ) {trans[i] = BigInteger.valueOf(idx++); retrans[idx-1]=i;} 50 for ( i='a' ; i<='z' ; i++ ) {trans[i] = BigInteger.valueOf(idx++); retrans[idx-1]=i;} 51 } 52 static public void main(String[] argStrings) throws FileNotFoundException 53 { 54 // System.setIn(new FileInputStream("test.txt")); 55 Scanner cin = new Scanner(System.in); 56 String str; 57 initTrans(); 58 int cas; 59 BigInteger base1,base2; 60 String ans; 61 cas = cin.nextInt(); 62 for (int i = 0; i < cas; i++) 63 { 64 base1 = cin.nextBigInteger(); 65 base2 = cin.nextBigInteger(); 66 str = cin.nextLine(); 67 ans = getAns(str, base1, base2); 68 System.out.println(base1+str); 69 System.out.println(base2+" "+ans); 70 System.out.println(); 71 } 72 System.exit(0); 73 } 74 }
JAVA中的重定向: