Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 471 | Accepted: 124 |
Description
Input
Output
Sample Input
35 1 121 1 1000730021 9
Sample Output
5 * 7 11 * 11 10007 * 100003
java大数功能很强大啊,有木有!可能,这里,是我认为,java比c++,好用的地方吧!想不通啊,为什么用c++写就非要超时,用java写这么容易就过了,java 是个好东西啊!题意不想理解错了,其实,就是,要把一个n可以分成两个数,把这两个数求出来,而且一定,是只有一个组合,没想到,这题这么坑啊!无语,但是学学java是个好东西啊!
import java.math.BigInteger; import java.util.Scanner; public class Main { static BigInteger n; static BigInteger k; public static void main(String[] args) { Scanner cin = new Scanner(System.in);//相当于scanf BigInteger one = BigInteger.ONE; BigInteger two = BigInteger.valueOf(2);//2要转换 BigInteger zero = BigInteger.ZERO; BigInteger l,r,tmp,mid; mid=zero; while(cin.hasNext())//相当于!=EOF { n = cin.nextBigInteger();//相当于scanf k = cin.nextBigInteger(); k = k.multiply(n); l = zero; r = k; while(l.compareTo(r)<=0) { mid = l.add(r).divide(two); tmp = mid.multiply(mid); if(tmp.compareTo(k) == 0) break; if(tmp.compareTo(k) < 0) l=mid.add(one); else r=mid.subtract(one); } BigInteger p,q; p=q= mid; while(true) { if(n.mod(p).equals(zero) && !p.equals(one) && !p.equals(n)) { q = n.divide(p); break; } if(n.mod(q).equals(zero)&& !q.equals(one) && !q.equals(n)) { p = n.divide(q); break; } p = p.add(one); q = q.subtract(one); } if(p.compareTo(q)>0) { tmp = p; p = q; q = tmp; } System.out.println(p+" * "+q); } } } 找了一些函数 JAVA大数处理(BigInteger,BigDecimal) 这两个类都在java.math.*包中,因此每次必须在开头处引用该包。 Ⅰ基本函数: 1.valueOf(parament); 将参数转换为制定的类型 比如 int a=3; BigInteger b=BigInteger.valueOf(a); 则b=3; String s=”12345”; BigInteger c=BigInteger.valueOf(s); 则c=12345; 2.add(); 大整数相加 BigInteger a=new BigInteger(“23”); BigInteger b=new BigInteger(“34”); a. add(b); 3.subtract(); 相减 4.multiply(); 相乘 5.divide(); 相除取整 6.remainder();取余 7.pow(); a.pow(b)=a^b//b不能为大数 8.gcd(); 最大公约数 9.abs(); 绝对值 10.negate();取反数 11.mod(); a.mod(b)=a%b=a.remainder(b); 13.punlic int comareTo(); 14.boolean equals(); 是否相等 15.BigInteger构造函数: 一般用到以下两种: BigInteger(String val); 将指定字符串转换为十进制表示形式; BigInteger(String val,int radix); 将指定基数的BigInteger的字符串表示形式转换为BigInteger Ⅱ.基本常量: A=BigInteger.ONE 1 B=BigInteger.TEN 10 C=BigInteger.ZERO 0 Ⅲ.基本操作 1. 读入: 用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中 Scanner cin=new Scanner(System.in);// 读入 while(cin.hasNext()) //等同于!=EOF { int n; BigInteger m; n=cin.nextInt(); //读入一个int; m=cin.BigInteger();//读入一个BigInteger; System.out.print(m.toString()); } Ⅳ.运用 四则预算: import java.util.Scanner; import java.math.*; import java.text.*; public class Main { public static void main(String args[]) { Scanner cin = new Scanner ( System.in ); BigInteger a,b; int c; char op; String s; while( cin.hasNext() ) { a = cin.nextBigInteger(); s = cin.next(); op = s.charAt(0); if( op == '+') { b = cin.nextBigInteger(); System.out.println(a.add(b)); } else if( op == '-') { b = cin.nextBigInteger(); System.out.println(a.subtract(b)); } else if( op == '*') { b = cin.nextBigInteger(); System.out.println(a.multiply(b)); } else { BigDecimal a1,b1,eps; String s1,s2,temp; s1 = a.toString(); a1 = new BigDecimal(s1); b = cin.nextBigInteger(); s2 = b.toString(); b1 = new BigDecimal(s2); c = cin.nextInt(); eps = a1.divide(b1,c,4); //System.out.println(a + " " + b + " " + c); //System.out.println(a1.doubleValue() + " " + b1.doubleValue() + " " + c); System.out.print( a.divide(b) + " " + a.mod(b) + " "); if( c != 0) { temp = "0."; for(int i = 0; i < c; i ++) temp += "0"; DecimalFormat gd = new DecimalFormat(temp); System.out.println(gd.format(eps)); } else System.out.println(eps); } } } import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { BigInteger n; BigInteger k; BigInteger c; Scanner cin = new Scanner(System.in);//相当于scanf BigInteger one = BigInteger.ONE; BigInteger two = BigInteger.valueOf(2);//2要转换 BigInteger zero = BigInteger.ZERO; BigInteger l,r,tmp,mid; mid=zero; double a; while(cin.hasNext())//相当于!=EOF { n = cin.nextBigInteger();//相当于scanf k = cin.nextBigInteger(); a = cin.nextDouble(); System.out.printf("%.3f\n",a);//double float 都要用f而不是lf System.out.println(n+""+k); c=n.add(k); System.out.println("add"+c); c=n.subtract(k); System.out.println("substrack"+c); c=n.multiply(k); System.out.println("multiply"+c); c=n.divide(k); System.out.println("divide"+c); c=n.remainder(k); System.out.println("remainder"+c); c=n.pow(2);//这里不能是大数 System.out.println("pow"+c); c=n.gcd(k); System.out.println("gcd"+c); c=n.abs(); System.out.println("abs"+c); c=n.negate(); System.out.println("negate"+c); System.out.printf("compare%d\n",n.compareTo(k)); //System.out.printf("equal%d\n", n.equals(k)); String mm="23232"; } } }