大数java来一波

来源:互联网 发布:js encodeuri编码 编辑:程序博客网 时间:2024/06/03 20:10

java大数计算

学了java许久不用,感觉也是忘的差不多了,今天来复习一波简单的java用法吧;

1.交oj时注意class名称必须用Main

2.输入

sc = new Scanner(System.in) ,sc. hasNext() // 检测是否运行到文件末尾

nextInt(), nextDouble()什么的就不用我多说了吧。

3.BigInteger类

BigInteger a = new BigInteger("1234567") 构造时里面用字符串就好了

或者BigInteger b  = BigInteger.valueOf(3)用int赋值也行啊

比较大小这个还是比较坑- -

不能直接用 ==,<=,>=之类的,还得用toString()之后进行比较或者用b.compareTo(a)进行比较;

最坑的就是居然b.add(a)还要赋值 mark一波, b = b.add(a);

//poj1503 AC代码import java.util.*;import java.math.*;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);BigInteger a, b = new BigInteger("0");while (sc.hasNext()) {a = sc.nextBigInteger();if (a.toString() == ("0"))break;b = b.add(a);}System.out.print(b.toString());}}

注意一下dp的方法就好了,dp[i][k] 表示以a[i]结尾的长度为k的上升子序列 dp[p][i] = sum(dp[q][i - 1]);(q = 0 : p - 1)

//HDOJ5568 AC代码(dp + 大数)import java.util.*;import java.math.*;public class Main {final static int maxn = 110;public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n, k;int[] a = new int[maxn];BigInteger[][] dp = new BigInteger[maxn][maxn];while (sc.hasNext()) {n = sc.nextInt();k = sc.nextInt();for (int i = 0; i < n; i++)dp[i][1] = new BigInteger("1");for (int i = 0; i < n; i++)a[i] = sc.nextInt();for (int i = 2; i <= k; i ++)for(int p = 0; p <= n - 1; p ++){dp[p][i] = new BigInteger("0");for(int q = 0; q <= p - 1; q ++){if(a[p] > a[q]) dp[p][i] = dp[p][i].add(dp[q][i - 1]);}}BigInteger result = new BigInteger("0");for(int i = 0; i <= n - 1; i ++){result = result.add(dp[i][k]);}System.out.println(result);}}}


0 0
原创粉丝点击