JAVA大数的运用

View Code
 1 import java.math.BigDecimal;
 2 import java.math.BigInteger;
 3 import java.util.Scanner;
 4 public class JAVA大数 {
 5     public static void main(String[] args) {
 6         Scanner sc = new Scanner(System.in); //输入   必加条件    
 7         BigInteger a,b; //定义两个大整数a b
 8         BigDecimal c,d; //定于两个浮点大数 c d
 9         BigInteger f[] = new BigInteger[100];        
10         //while(sc.hasNext()){//读文件结束        }    
11         a  = sc.nextBigInteger();//获取一个大数
12         b = sc.nextBigInteger();//获取一个大数
13         c = sc.nextBigDecimal();//获取一个大浮点数
14         d = sc.nextBigDecimal();//获取一个大浮点数
15         //以下返回的都是对应的类型
16         System.out.println(a.add(b));//输出a + b
17         System.out.println(c.add(d));//输出c + d        
18         System.out.println(a.subtract(b));//输出a - b
19         System.out.println(a.multiply(b));//输出 a * b
20         System.out.println(a.divide(b));//输出 a / b
21         System.out.println(c.divide(d));//输出c / d
22         System.out.println(a.mod(b));//输出a % b
23         System.out.println(a.pow(3));//输出a 的 x次幂
24         /*
25                  sc.next() //读一个字符串
26                  sc.nextInt()读一个整数
27          */
28         if(a.compareTo(b) == 0){//a == b
29             System.out.println(a + "等 于" + b);
30         }else if(a.compareTo(b) > 0){//a > b
31             System.out.println(a + "大于" + b);
32         }else{//a < b
33             System.out.println(a + "小于" + b);
34         }
35         //其他数组操作类似
36         f[0] = BigInteger.ZERO;//f[0]为0
37         f[1] = BigInteger.ONE;//f[1]为1
38         //肥波纳妾
39         for(int i = 2; i < 100; i++){
40             f[i] = f[i-1].add(f[i-2]);
41         }
42         System.out.println(f[99]);        
43     }
44 }
posted @ 2012-10-06 16:14  Wheat″  阅读(261)  评论(0)    收藏  举报