zoukankan      html  css  js  c++  java
  • Java BigInteger(大数,ACM比赛专用)

    用c或者C++处理大数比较麻烦,于是决心学习一下JAVA中大数运算。

    先下载一个eclipse,具体的用法去问度娘吧

    JAVA中有两个类BigIntegerBigDecimal分别表示大整数类和大浮点数类

    这两个类都在java.math.*包中,因此每次必须在开头处引用该包(import java.math.*)。

    下面说说几个常用的用法

    1. 

       int a=3;

      BigInteger b=BigInteger.valueOf(a);

      则b=3;

    2.

    String s="-123459999999999999999999";
    BigInteger c=new BigInteger(s,10);

    把字符串转换成10进制的大数;

    3.

    BigInteger a=new BigInteger("234");
    BigInteger b=new BigInteger("567");

    System.out.println(a.add(b));

    2个大数相加,a没变。

    subtract(); 相减   multiply(); 相乘   divide();    相除取整   remainder(); 取余  pow();   a.pow(b)=a^b  gcd();   最大公约数

    abs(); 绝对值  negate(); 取反数  mod(); a.mod(b)=a%b=a.remainder(b);   max(); min();  boolean equals(); 是否相等

    3.

    读入:

    用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中

    Scanner cin=new Scanner(System.in);
    while(cin.hasNext())//相当于EOF
    {
    int n;
    BigInteger m;
    n=cin.nextInt();
    m=cin.nextBigInteger();
    System.out.println(n);
    System.out.println(m);
    }

    插入写的代码

     1 import java.math.BigInteger;
     2 import java.util.Scanner;
     3 public class hello {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7          //System.out.println("hello");
     8         //int num=3;
     9         //System.out.println("hell0"+num);
    10        //int a=3;
    11        //BigInteger b=BigInteger.valueOf(a);
    12        //System.out.println(b);
    13        //String s="-123459999999999999999999";
    14        //BigInteger c=new BigInteger(s,10);
    15        //BigInteger c=new BigInteger("12345"); 
    16         //BigInteger a=new BigInteger("234");
    17         //BigInteger b=new BigInteger("567");
    18         //BigInteger c=a+b;
    19         //subtract();
    20         //multiply();
    21         //divide();
    22         //remainder();
    23         //pow();
    24         //gcd();
    25         //System.out.println(a.add(b));
    26         Scanner cin=new Scanner(System.in);
    27         while(cin.hasNext())
    28         {
    29             //int n;
    30             BigInteger m,n;
    31             n=cin.nextBigInteger();
    32             m=cin.nextBigInteger();
    33             boolean coper=n.equals(m);
    34             System.out.println(n);
    35             System.out.println(m);
    36             System.out.println(coper);
    37         }
    38        
    39     }
    40 }
  • 相关阅读:
    linux系统日志及其rsyslog服务
    C++
    程序员之---C语言细节18(一些奇怪表达式)
    Spring MVC的简单使用方法
    Android系统开发(4)——Autotools
    大话设计模式C++版——代理模式
    JS获取地址栏并拼接參数
    二叉树的应用(1)--二叉树排序树基本操作
    【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】
    Android 实现形态各异的双向側滑菜单 自己定义控件来袭
  • 原文地址:https://www.cnblogs.com/tsw123/p/4378697.html
Copyright © 2011-2022 走看看