ACM 大数问题函数及应用
来源:互联网 发布:域名和空间费用 编辑:程序博客网 时间:2024/06/11 00:49
大数问题
在用C或者C++处理大数时感觉非常麻烦,但是在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^b8.gcd(); 最大公约数9.abs(); 绝对值10.negate();取反数11.mod(); a.mod(b)=a%b=a.remainder(b);12.max(); min();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); } } }}关于ACM中应用的一些问题:(1) JDK 1.5.0 及其以上版本提供的Scanner类为输入提供了良好的基础,很好地优化Java的输入问题。 代码如下: import java.io.* import java.util.* public class Main { public static void main(String args[]) { Scanner cin = new Scanner(new BufferedInputStream(System.in)); } } 也可以直接 Scanner cin = new Scanner(System.in); 加Buffer可能会快一些。(2) 读一个整数: int n = cin.nextInt(); 相当于 scanf("%d", &n); 或 cin >> n; 读一个字符串: String s = cin.next(); 相当于 scanf("%s", s); 或 cin >> s; 读一个浮点数: double t = cin.nextDouble(); 相当于 scanf("%lf", &t); 或 cin >> t; 读一整行: String s = cin.nextLine(); 相当于 gets(s); 或 cin.getline(...); 判断是否有下一个输入可以用 cin.hasNext() 或 cin.hasNextInt() 或 cin.hasNextDouble()(3) 输出一般可以直接用 System.out.print() 和 System.out.println(),前者不输出换行,而后者输出。 System.out.println(n); // n 为 int 型 同一行输出多个整数可以用 System.out.println(new Integer(n).toString() + " " + new Integer(m).toString()); //也可重新定义: static PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out)); cout.println(n); (4)对于输出浮点数保留几位小数的问题,可以使用DecimalFormat类, import java.text.*; DecimalFormat f = new DecimalFormat("#.00#"); DecimalFormat g = new DecimalFormat("0.000"); double a = 123.45678, b = 0.12; System.out.println(f.format(a)); System.out.println(f.format(b)); System.out.println(g.format(b)); 大数:BigInteger 和 BigDecimal 是在java.math包中已有的类,前者表示整数,后者表示浮点数 import java.math.* // 需要引入 java.math 包 BigInteger a = BigInteger.valueOf(100); BigInteger b = BigInteger.valueOf(50); BigInteger c = a.add(b) // c = a + b; //主要有以下方法可以使用: BigInteger add(BigInteger other) BigInteger subtract(BigInteger other) BigInteger multiply(BigInteger other) BigInteger divide(BigInteger other) BigInteger mod(BigInteger other) int compareTo(BigInteger other) static BigInteger valueOf(long x) //输出数字时直接使用 System.out.println(a) 即可字符串:String 类用来存储字符串,可以用charAt方法来取出其中某一字节,计数从0开始:String a = "Hello"; // a.charAt(1) = 'e'用substring方法可得到子串,如上例System.out.println(a.substring(0, 4)) // output "Hell"注意第2个参数位置上的字符不包括进来。这样做使得 s.substring(a, b) 总是有 b-a个字符。字符串连接可以直接用 + 号,如String a = "Hello"; String b = "world"; System.out.println(a + ", " + b + "!"); // output "Hello, world!"如想直接将字符串中的某字节改变,可以使用另外的StringBuffer类。调用递归(或其他动态方法)在主类中 main 方法必须是 public static void 的,在 main 中调用非static类时会有警告信息, 可以先建立对象,然后通过对象调用方法:public class Main { void dfs(int a) { if () return; dfs(a+1); } public static void main(String args[]) { Main e = new Main(); e.dfs(0); } } 其他注意的事项:(1) Java 是面向对象的语言,思考方法需要变换一下,里面的函数统称为方法,不要搞错。(2) Java 里的数组有些变动,多维数组的内部其实都是指针,所以Java不支持fill多维数组。 数组定义后必须初始化,如 int[] a = new int[100];(3) 布尔类型为 boolean,只有true和false二值,在 if (...) / while (...) 等语句的条件中必须为boolean类型。 在C/C++中的 if (n % 2) ... 在Java中无法编译通过。(4) 下面在java.util包里Arrays类的几个方法可替代C/C++里的memset、qsort/sort 和 bsearch:Arrays.fill() Arrays.sort() Arrays.binarySearch() 虽然Java功能很强大,但不能完全依赖他,毕竟C和C++还是ACM/ICPC的主流语言,适当地使用才能有效提高比赛中的成绩。。。
0 0
- ACM 大数问题函数及应用
- ACM内部函数--数学问题--大数相加
- ACM内部函数--数学问题--大数阶乘
- ACM内部函数--数学问题--大数相乘
- acm 1012 大数问题
- ACM大数阶乘问题
- ACM内部函数--数学问题--大数乘小数
- ACM内部函数--数学问题--大数相减
- ACM内部函数--数学问题--大数除法和取余
- 杭电acm 大数问题
- JAVA 大数在acm中的应用
- ACM-Java中大数的应用
- java大数类在acm中的应用
- JAVA大数在ACM中应用
- ACM 大数问题的知识点与算法
- ACM 大数 大数阶乘
- GAE 时区问题及模板函数应用
- JAVA 大数在acm中的应用(2)
- 通过jQuery Ajax使用FormData对象上传文件
- Final关键字
- Java学习笔记代码
- JZOJ1249.【USACO题库】1.3.4 Prime Cryptarithm牛式
- 用Python实现的简洁版的用户登录程序
- ACM 大数问题函数及应用
- 2017-4-20
- python web 部署:nginx + gunicorn + supervisor + flask 部署笔记
- 1001. 害死人不偿命的(3n+1)猜想 (15)
- 如何评价 2017 年 3 月发布的 Android O 预览版?
- 父类 子类 动态代码块 静态代码块 构造函数 执行顺序
- 架构师之路--线程
- form表单中的encType属性
- getline()函数