java写大数总结
2016-07-11 23:25
447 查看
把一些涉及到大数的水题写了一番,以后遇到大数还是java大法方便啊。
HDU 1002
a+b大数版
import java.math.BigInteger; import java.util.Scanner; public class Main { void solve () { BigInteger a, b, c; Scanner cin = new Scanner(System.in); int t = cin.nextInt (); for (int i = 1; i <= t; i++) { System.out.println ("Case " + i + ":"); a = cin.nextBigInteger (); b = cin.nextBigInteger (); System.out.println (a + " + " + b + " = " + a.add (b)); if (i != t) System.out.println (); } } public static void main (String[] args) { Main work = new Main(); work.solve (); } }
HDU 1042
阶乘大数版
import java.math.BigInteger; import java.util.Scanner; public class Main { int maxn = 10005; void solve () { Scanner cin = new Scanner(System.in); int n; while (cin.hasNext()) { n = cin.nextInt (); BigInteger ans = BigInteger.valueOf (1); for (int i = 2; i <= n; i++) { ans = ans.multiply (BigInteger.valueOf (i)); } System.out.println (ans); } } public static void main (String[] args) { Main work = new Main(); work.solve (); } }
HDU 1250
斐波那契数列大数版
import java.math.BigInteger; import java.util.Scanner; public class Main { void solve () { Scanner cin = new Scanner(System.in); BigInteger f1, f2, f3, f4, ans; while (cin.hasNext ()) { int n = cin.nextInt (); f1 = BigInteger.valueOf (1); f2 = f3 = f4 = ans = f1; if (n <= 4) { System.out.println ("1"); continue; } for (int j = 5; j <= n; j++) { ans = f1.add (f2.add (f3.add (f4))); f1 = f2; f2 = f3; f3 = f4; f4 = ans; } System.out.println (ans); } } public static void main (String[] args) { Main work = new Main(); work.solve (); } }
HDU 1297
f(n)=f(n−1)+f(n−2)+f(n−4)
import java.math.*; import java.util.*; public class Main { void solve () { Scanner cin = new Scanner(System.in); BigInteger[] ans = new BigInteger[1001]; ans[1] = BigInteger.valueOf (1); ans[2] = BigInteger.valueOf (2); ans[3] = BigInteger.valueOf (4); ans[4] = BigInteger.valueOf (7); for (int i = 5; i <= 1000; i++) { ans[i] = ans[i-1].add (ans[i-2].add (ans[i-4])); } while (cin.hasNext ()) { int n = cin.nextInt (); System.out.println (ans ); } } public static void main (String[] args) { Main work = new Main(); work.solve (); } }
HDU 1715
还是斐波那契数列
import java.math.BigInteger; import java.util.Scanner; public class Main { void solve () { Scanner cin = new Scanner(System.in); int t = cin.nextInt (); BigInteger f1, f2, f3; for (int i = 0; i < t; i++) { int n = cin.nextInt (); f1 = BigInteger.valueOf (1); f2 = BigInteger.valueOf (1); f3 = BigInteger.valueOf (0); if (n == 1 || n == 2) { System.out.println ("1"); continue; } for (int j = 3; j <= n; j++) { f3 = f1.add (f2); f1 = f2; f2 = f3; } System.out.println (f3); } } public static void main (String[] args) { Main work = new Main(); work.solve (); } }
HDU 1753
高精度小数,要去掉末尾的后导0.
import java.math.*; import java.util.*; public class Main { void solve () { //BigInteger a, b, c; Scanner cin = new Scanner(System.in); BigDecimal a = BigDecimal.valueOf (0); BigDecimal b = BigDecimal.valueOf (0); while (cin.hasNext ()) { a = cin.nextBigDecimal (); b = cin.nextBigDecimal (); System.out.println (a.add (b).stripTrailingZeros().toPlainString()); } } public static void main (String[] args) { Main work = new Main(); work.solve (); } }
HDU 1865
f(n)=f(n−1)+f(n−2)
import java.math.*; import java.util.*; public class Main { void solve () { BigInteger a, b, c; Scanner cin = new Scanner(System.in); int t = cin.nextInt (); String s; for (int l = 1; l <= t; l++) { s = cin.next (); int n = s.length (); if (n == 1 || n == 2) { System.out.println (n); continue; } BigInteger f1 = BigInteger.valueOf (1); BigInteger f2 = BigInteger.valueOf (2); BigInteger f3 = BigInteger.valueOf (0); for (int i = 3; i <= n; i++) { f3 = f1.add (f2); f1 = f2; f2 = f3; } System.out.println (f3); } } public static void main (String[] args) { Main work = new Main(); work.solve (); } }
相关文章推荐
- springMVC初次搭建,产生错误
- Myeclipse2014安装activiti工作流插件
- springmvc搭建返回json格式的restful api
- jdk环境变量的配置
- selenium1、selenium2 示例代码(java版)
- 【Java】继承和多态
- 一台机子可以装两种版本的JDK吗?
- 9. 【对于所有对象都通用的方法】重写equals方法时一定也要重写hashCode方法
- leetcode 102. Binary Tree Level Order Traversal
- Eclipse代码补全,修改 空格键 "=" 键不上屏
- Java 集合 HashMap & HashSet 拾遗
- java的一路发展历程!
- java是一门有未来的语言!
- spring四种依赖注入方式 (set,构造器,工厂,注解 )
- SpringMvc的入门案例
- 归并排序-java实现
- JMX的Hello World
- JAVA设计模式之单例模式
- 【Spring】对象后期处理,BeanPostProcessor
- SpringMVC学习笔记