学下java 的大数该怎么用><
hdu 1023 Train Problem II
求 卡特兰 数
诶...不记得卡特兰数的我眼泪掉下来
第一次用 java 大数 有点激动...><
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.*; 2 import java.util.*; 3 import java.math.BigInteger; 4 5 public class test{ 6 public static void main(String args[]){ 7 BigInteger[] a = new BigInteger[101]; 8 a[0] = BigInteger.ZERO; 9 a[1] = BigInteger.ONE; 10 for(int i = 2;i <= 100;i++){ 11 BigInteger l = BigInteger.valueOf(4*i-2); 12 BigInteger r = BigInteger.valueOf(i+1); 13 a[i] = a[i-1].multiply(l); 14 a[i] = a[i].divide(r); 15 } 16 Scanner in = new Scanner(System.in); 17 int n; 18 while(in.hasNext()){ 19 n = in.nextInt(); 20 System.out.println(a[n]); 21 } 22 } 23 24 }
hdu 1063 Exponentiation
求小数 a 的 b 次方
stripTrailingZeros:去掉末尾0
toPlainStrin:将科学计数法表示正常写法
startWith:去掉首位0
subtring(x) :截取从 第 x 位往后的字符串
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.*; 2 import java.util.*; 3 import java.math.BigDecimal; 4 import java.math.BigInteger; 5 6 public class test{ 7 public static void main(String args[]){ 8 Scanner in = new Scanner(System.in); 9 while(in.hasNext()){ 10 BigDecimal a = in.nextBigDecimal(); 11 int b = in.nextInt(); 12 String ans = a.pow(b).stripTrailingZeros().toPlainString(); 13 if(ans.startsWith("0")){ 14 ans = ans.substring(1); 15 } 16 System.out.println(ans); 17 } 18 } 19 20 }
不过看C++的都是 0 ms 过,java要跑 500多ms
hdu 1047 Integer Inquiry
大数相加
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.*; 2 import java.util.*; 3 import java.math.BigDecimal; 4 import java.math.BigInteger; 5 6 public class Main{ 7 public static void main(String args[]){ 8 Scanner in = new Scanner(System.in); 9 int n = in.nextInt(); 10 while(n-- > 0){ 11 BigInteger ans = BigInteger.ZERO; 12 while(in.hasNext()){ 13 BigInteger tmp = BigInteger.ZERO; 14 tmp = in.nextBigInteger(); 15 BigInteger zero = BigInteger.valueOf(0); 16 if(!tmp.equals(zero)){ 17 ans = ans.add(tmp); 18 } 19 else{ 20 System.out.println(ans); 21 if(n != 0) System.out.println(""); 22 break; 23 } 24 } 25 } 26 } 27 }
hdu 1316 How Many Fibs?
统计大数区间里面的斐波那契数的个数
用到 一个 compareto
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.*; 2 import java.util.*; 3 import java.math.BigDecimal; 4 import java.math.BigInteger; 5 6 public class Main{ 7 public static void main(String args[]){ 8 Scanner in = new Scanner(System.in); 9 BigInteger f[] = new BigInteger[505]; 10 BigInteger zero = BigInteger.ZERO; 11 f[1] = new BigInteger("1"); 12 f[2] = new BigInteger("2"); 13 for(int i = 3;i <= 500;i++) f[i] = f[i-1].add(f[i-2]); 14 15 while(in.hasNext()){ 16 BigInteger l = in.nextBigInteger(); 17 BigInteger r = in.nextBigInteger(); 18 if(l.compareTo(zero) == 0 && r.compareTo(zero) == 0) break; 19 int ans = 0; 20 for(int i = 1;i <= 500;i++){ 21 if(l.compareTo(f[i]) <= 0 && r.compareTo(f[i]) >= 0) ans++; 22 } 23 System.out.println(ans); 24 } 25 } 26 }
hdu 1753 大明A+B
大实数相加
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.*; 2 import java.util.*; 3 import java.math.BigDecimal; 4 public class Main { 5 public static void main(String[] args) 6 { 7 Scanner in=new Scanner(System.in); 8 while (in.hasNext()) 9 { 10 BigDecimal a=in.nextBigDecimal(); 11 BigDecimal b=in.nextBigDecimal(); 12 a=a.add(b); 13 String s=a.stripTrailingZeros().toPlainString(); 14 // if (s.startsWith("0.")) 15 // s=s.substring(1); 16 System.out.println(s); 17 } 18 } 19 }
hdu 1261 字串数
大数阶乘,除法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.*; 2 import java.util.*; 3 import java.math.BigDecimal; 4 import java.math.BigInteger; 5 6 public class Main{ 7 public static void main(String args[]){ 8 Scanner in = new Scanner(System.in); 9 int[] a = new int[55]; 10 BigInteger[] f = new BigInteger[300]; 11 f[0] = BigInteger.ONE; 12 for(int i = 1;i < 300;i++) { 13 BigInteger tmp = BigInteger.valueOf(i); 14 f[i] = f[i-1].multiply(tmp); 15 } 16 int n; 17 while(in.hasNext()){ 18 n = in.nextInt(); 19 if(n == 0) break; 20 int x; 21 BigInteger fz ,fm; 22 fm = BigInteger.ONE; 23 int fzz = 0,fmm = 0; 24 for(int i = 1;i <= n;i++){ 25 a[i] = in.nextInt(); 26 fzz = fzz + a[i]; 27 fm = fm.multiply(f[a[i]]); 28 } 29 fz = f[fzz]; 30 //System.out.println(fz); 31 //System.out.println(fm); 32 BigInteger ans = fz.divide(fm); 33 System.out.println(ans); 34 } 35 } 36 }
java 板
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //卡特兰数 2 import java.io.*; 3 import java.util.*; 4 import java.math.BigInteger; 5 6 public class test{ 7 public static void main(String args[]){ 8 BigInteger[] a = new BigInteger[101]; 9 a[0] = BigInteger.ZERO; 10 a[1] = BigInteger.ONE; 11 for(int i = 2;i <= 100;i++){ 12 BigInteger l = BigInteger.valueOf(4*i-2); 13 BigInteger r = BigInteger.valueOf(i+1); 14 a[i] = a[i-1].multiply(l); 15 a[i] = a[i].divide(r); 16 } 17 Scanner in = new Scanner(System.in); 18 int n; 19 while(in.hasNext()){ 20 n = in.nextInt(); 21 System.out.println(a[n]); 22 } 23 } 24 25 } 26 27 /*求小数 a 的 b 次方 28 stripTrailingZeros:去掉末尾0 29 toPlainStrin:将科学计数法表示正常写法 30 startWith:去掉首位0 31 subtring(x) :截取从 第 x 位往后的字符串*/ 32 33 import java.io.*; 34 import java.util.*; 35 import java.math.BigDecimal; 36 import java.math.BigInteger; 37 38 public class test{ 39 public static void main(String args[]){ 40 Scanner in = new Scanner(System.in); 41 while(in.hasNext()){ 42 BigDecimal a = in.nextBigDecimal(); 43 int b = in.nextInt(); 44 String ans = a.pow(b).stripTrailingZeros().toPlainString(); 45 if(ans.startsWith("0")){ 46 ans = ans.substring(1); 47 } 48 System.out.println(ans); 49 } 50 } 51 52 } 53 54 //大数相加 55 import java.io.*; 56 import java.util.*; 57 import java.math.BigDecimal; 58 import java.math.BigInteger; 59 60 public class Main{ 61 public static void main(String args[]){ 62 Scanner in = new Scanner(System.in); 63 int n = in.nextInt(); 64 while(n-- > 0){ 65 BigInteger ans = BigInteger.ZERO; 66 while(in.hasNext()){ 67 BigInteger tmp = BigInteger.ZERO; 68 tmp = in.nextBigInteger(); 69 BigInteger zero = BigInteger.valueOf(0); 70 if(!tmp.equals(zero)){ 71 ans = ans.add(tmp); 72 } 73 else{ 74 System.out.println(ans); 75 if(n != 0) System.out.println(""); 76 break; 77 } 78 } 79 } 80 } 81 } 82 83 //大实数相加 84 import java.io.*; 85 import java.util.*; 86 import java.math.BigDecimal; 87 public class Main { 88 public static void main(String[] args) 89 { 90 Scanner in=new Scanner(System.in); 91 while (in.hasNext()) 92 { 93 BigDecimal a=in.nextBigDecimal(); 94 BigDecimal b=in.nextBigDecimal(); 95 a=a.add(b); 96 String s=a.stripTrailingZeros().toPlainString(); 97 // if (s.startsWith("0.")) 98 // s=s.substring(1); 99 System.out.println(s); 100 } 101 } 102 } 103 104 //大数阶乘,除法 105 import java.io.*; 106 import java.util.*; 107 import java.math.BigDecimal; 108 import java.math.BigInteger; 109 110 public class Main{ 111 public static void main(String args[]){ 112 Scanner in = new Scanner(System.in); 113 int[] a = new int[55]; 114 BigInteger[] f = new BigInteger[300]; 115 f[0] = BigInteger.ONE; 116 for(int i = 1;i < 300;i++) { 117 BigInteger tmp = BigInteger.valueOf(i); 118 f[i] = f[i-1].multiply(tmp); 119 } 120 int n; 121 while(in.hasNext()){ 122 n = in.nextInt(); 123 if(n == 0) break; 124 int x; 125 BigInteger fz ,fm; 126 fm = BigInteger.ONE; 127 int fzz = 0,fmm = 0; 128 for(int i = 1;i <= n;i++){ 129 a[i] = in.nextInt(); 130 fzz = fzz + a[i]; 131 fm = fm.multiply(f[a[i]]); 132 } 133 fz = f[fzz]; 134 //System.out.println(fz); 135 //System.out.println(fm); 136 BigInteger ans = fz.divide(fm); 137 System.out.println(ans); 138 } 139 } 140 } 141 142 /*主类名字必须是Main 143 读一个整数: int n = cin.nextInt(); 144 读一个字符串:String s = cin.next(); 145 读一个浮点数:double t = cin.nextDouble(); 146 读一整行: String s = cin.nextLine(); 147 判断是否有下一个输入可以用 cin.hasNext() 或 cin.hasNextInt() 或 cin.hasNextDouble() 等 148 149 输出浮点数保留位数 150 DecimalFormat f = new DecimalFormat("#.00#"); 151 DecimalFormat g = new DecimalFormat("0.000"); 152 double a = 123.45678, b = 0.12; 153 System.out.println(f.format(a)); 154 System.out.println(f.format(b)); 155 System.out.println(g.format(b)); 156 157 大数的运算 158 BigInteger add(BigInteger other) 159 BigInteger subtract(BigInteger other) 160 BigInteger multiply(BigInteger other) 161 BigInteger divide(BigInteger other) 162 BigInteger mod(BigInteger other) 163 int compareTo(BigInteger other) 164 static BigInteger valueOf(long x) 165 166 输出直接用 System.out.println(a)*/ 167 168 StringBuffer 169 import java.io.*; 170 import java.util.*; 171 import java.text.*; 172 import java.math.BigDecimal; 173 import java.math.BigInteger; 174 175 public class test{ 176 public static void main(String args[]){ 177 //赋值 178 StringBuffer a = new StringBuffer();//初始化 179 StringBuffer b = new StringBuffer("abc"); 180 String c = "abc"; 181 StringBuffer d = new StringBuffer(c); 182 183 //在末尾加 184 StringBuffer e = new StringBuffer("abc"); 185 e.append(true); 186 187 //删除指定index 位置的字符 188 StringBuffer f = new StringBuffer("abc"); 189 f.deleteCharAt(1); 190 191 //删除指定区间的字符 192 StringBuffer g = new StringBuffer("abcdefghjjkl"); 193 g.delete(1,4); 194 195 //在index 位置插入字符串或者字符 196 StringBuffer h = new StringBuffer("huhhhuhuhuhhh"); 197 String tmp = "njjjjjjj"; 198 char ch = 'c'; 199 h.insert(4,ch); 200 h.insert(4,tmp); 201 202 //字符串反转 203 StringBuffer j = new StringBuffer("12345"); 204 j.reverse(); 205 206 //替换字符串 207 StringBuffer i = new StringBuffer("122222"); 208 i.replace(0, 3, "ab");//左闭右开 209 210 //子串 211 StringBuffer kk = new StringBuffer("1234567"); 212 String kk1 = kk.substring(2);//返回下标开始以后的字符串 213 String kk2 = kk.substring(2,4);//l 到 r-1的字符串 214 System.out.println(kk1); 215 System.out.println(kk2); 216 } 217 }