(部分转载,部分原创)java大数类(2)
NYOJ 773 ?開方數(shù)
http://acm.nyist.net/JudgeOnline/problem.php?pid=773
1 import java.util.Scanner; 2 3 public class Main{ 4 public static void main(String[] args){ 5 int n; 6 double p; 7 Scanner cin = new Scanner(System.in); 8 while(true){ 9 n = cin.nextInt(); 10 p = cin.nextDouble(); 11 if(n == 0 && p == 0) break; 12 System.out.printf("%.0f\n", Math.pow(p, 1.0/n)); 13 } 14 } 15 } View Code?
POJ 1001?Exponentiation
http://poj.org/problem?id=1001
1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 import java.text.*; 5 6 public class Main 7 { 8 public static void main(String[] args) 9 { 10 Scanner cin = new Scanner(System.in); 11 while(cin.hasNext()) 12 { 13 BigDecimal R = cin.nextBigDecimal(); 14 int n = cin.nextInt(); 15 String ans = R.pow(n).stripTrailingZeros().toPlainString(); 16 if(ans.startsWith("0")) 17 ans = ans.substring(1); 18 System.out.println(ans); 19 } 20 } 21 } View Code?
HDU 1042 ?N!
http://acm.hdu.edu.cn/showproblem.php?pid=1042
java版:
1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 12 14 13 15 14 16 15 17 16 18 17 import java.util.Scanner; 18 import java.math.BigInteger; 19 public class Main{ 20 public static void main(String[] args){ 21 Scanner input = new Scanner(System.in); 22 int A, i; 23 while(input.hasNext()) 24 { 25 A = input.nextInt(); 26 BigInteger B = BigInteger.ONE; 27 for(i=1; i<=A; i++) 28 { 29 B = B.multiply(BigInteger.valueOf(i)); 30 } 31 System.out.println(B); 32 } 33 } 34 } View CodeC++版:
1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 12 14 13 15 14 16 15 17 16 18 17 19 18 20 19 21 20 22 21 23 22 24 23 25 24 26 25 27 26 28 27 29 28 30 29 31 30 32 31 33 32 34 33 35 34 36 35 37 36 38 37 39 38 40 39 41 40 42 41 43 42 44 43 45 44 46 45 47 46 48 47 49 48 #include<iostream> 49 #include<string.h> 50 #include<stdio.h> 51 #include<ctype.h> 52 #include<algorithm> 53 #include<stack> 54 #include<queue> 55 #include<set> 56 #include<map> 57 #include<math.h> 58 #include<vector> 59 #include<deque> 60 #include<list> 61 #define INF 0x7fffffff 62 #define inf 0x3f3f3f3f 63 #define maxn 40000 64 using namespace std; 65 int a[maxn]; 66 int main() 67 { 68 int n; 69 int s; 70 while(scanf("%d",&n)!=EOF) 71 { 72 memset(a,0,sizeof(a)); 73 a[0]=1; 74 for(int i=2; i<=n; i++) 75 { 76 int c=0; 77 for(int j=0; j<maxn; j++) 78 { 79 s=a[j]*i+c; 80 a[j]=s%10; 81 c=s/10; 82 } 83 } 84 int p; 85 for(int i=maxn; i>=0; i--) 86 if(a[i]) 87 { 88 p=i; 89 break; 90 } 91 for(int i=p; i>=0; i--) 92 printf("%d",a[i]); 93 printf("\n"); 94 } 95 return 0; 96 } View Code?
?
FZOJ 1602 ?Best results
http://acm.fzu.edu.cn/problem.php?pid=1602
1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 import java.text.*; 5 6 public class Main 7 { 8 public static void main(String[] args) 9 { 10 Scanner cin = new Scanner(System.in); 11 while(cin.hasNext()) 12 { 13 int t = cin.nextInt(); 14 BigDecimal ans = BigDecimal.ONE; 15 while(t-- > 0) 16 { 17 String str = cin.next(); 18 int l = str.length(); 19 str = str.substring(0, l - 1); 20 int tep = Integer.parseInt(str); 21 BigDecimal Tep = BigDecimal.valueOf(tep); 22 Tep = Tep.divide(BigDecimal.valueOf(100)); 23 ans = ans.multiply(Tep); 24 } 25 System.out.println(ans.stripTrailingZeros().toPlainString()); 26 } 27 } 28 } View Code?
?
ZOJ ?3549 ??Little Keng
一開始超時(shí)了,百撕不得騎姐,后來意識(shí)到可能JAVA計(jì)算大數(shù)和小數(shù)的時(shí)候,復(fù)雜度還是差別挺大的,雖然理論上都是O(1)的操作,但是當(dāng)數(shù)的規(guī)模增長到一定級(jí)數(shù),運(yùn)算效率的差別就顯露出來了。
不超時(shí)的關(guān)鍵就是快速冪的時(shí)候加上取模的操作。
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3549
1 import java.math.*; 2 import java.util.*; 3 4 public class Main { 5 static BigInteger power(BigInteger p,BigInteger n,BigInteger m) //快速冪 6 { 7 BigInteger sq=BigInteger.ONE; 8 while(n.compareTo(BigInteger.ZERO)>0) //while(n>0) 9 { 10 if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ONE)==0) //if(n%2==1) 11 sq=sq.multiply(p).mod(m); // sq=(sq*p)%m; 12 p=p.multiply(p).mod(m); //p=(p*p)%m; 13 n=n.divide(BigInteger.valueOf(2)); //n=n/2; 14 } 15 return sq.mod(m); 16 } 17 static boolean judge(int m,int n,int k) 18 { 19 BigInteger mod=BigInteger.ONE,ans=BigInteger.ZERO; 20 int i; 21 for(i=1;i<=k;i++) //10^k 22 mod=mod.multiply(BigInteger.valueOf(10)); 23 for(i=1;i<=m;i++) //1^n+2^n+3^n+...+m^n 24 { 25 BigInteger a,b; 26 a=BigInteger.valueOf(i); 27 b=BigInteger.valueOf(n); 28 ans=(ans.add(power(a,b,mod))).mod(mod); 29 } 30 if(ans.mod(mod).compareTo(BigInteger.ZERO)==0) return true; 31 else return false; 32 } 33 public static void main(String args[]) 34 { 35 Scanner cin=new Scanner(System.in); 36 int i,m,n; 37 while(cin.hasNext()) 38 { 39 m=cin.nextInt(); 40 n=cin.nextInt(); 41 for(i=1;;i++) 42 { 43 if(judge(m,n,i)) continue; 44 else break; 45 } 46 System.out.println(i-1); 47 } 48 } 49 } View Code這是正解。。。
?
1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 import java.text.*; 5 6 class Main 7 { 8 /* static BigInteger Pow(BigInteger a, int b) 9 { 10 BigInteger ans = BigInteger.ONE; 11 while(b > 0) 12 { 13 if(b % 2 == 1) 14 ans = ans.multiply(a); 15 ans = ans.multiply(ans); 16 b >>= 1; 17 //b /= 2; 18 } 19 return ans; 20 } */ 21 22 23 static BigInteger power(BigInteger p,BigInteger n) //快速冪 24 { 25 BigInteger sq=BigInteger.ONE; 26 while(n.compareTo(BigInteger.ZERO)>0) //while(n>0) 27 { 28 if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ONE)==0) //if(n%2==1) 29 sq=sq.multiply(p); // sq=(sq*p)%m; 30 p=p.multiply(p); //p=(p*p)%m; 31 n=n.divide(BigInteger.valueOf(2)); //n=n/2; 32 } 33 return sq; 34 } 35 36 public static void main(String[] args) 37 { 38 Scanner cin = new Scanner (System.in); 39 while(cin.hasNext()) 40 { 41 int m = cin.nextInt(); 42 int n = cin.nextInt(); 43 44 //System.out.println(Pow(BigInteger.TEN, 1)); 45 46 BigInteger ans = BigInteger.ZERO; 47 for(int i = 1; i <= m; ++i) 48 { 49 BigInteger I = BigInteger.valueOf(i); 50 BigInteger N = BigInteger.valueOf(n); 51 ans = ans.add(power(I, N)); 52 //ans = ans.add(Pow(I, n)); 53 } 54 int cnt = 0; 55 while(ans.mod(BigInteger.TEN).compareTo(BigInteger.ZERO) == 0) 56 { 57 ans = ans.divide(BigInteger.TEN); 58 ++cnt; 59 } 60 61 System.out.println(cnt); 62 } 63 //System.out.println("Hello World!"); 64 } 65 } View Code這是TLE的代碼。。。
轉(zhuǎn)載于:https://www.cnblogs.com/qiucz/p/4396848.html
總結(jié)
以上是生活随笔為你收集整理的(部分转载,部分原创)java大数类(2)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 批处理通过字符串截取得到文件名
- 下一篇: 软引用、弱引用、虚引用