zoukankan      html  css  js  c++  java
  • java 大数计算

    这几天做了几道用大数的题,发现java来做大数运算十分方便。对acmer来说是十分实用的

    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^b

    8.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构造函数:

     1 import java.io.*;
     2 import java.math.BigInteger;
     3 import java.util.*;
     4 public class Main {
     5 
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         Scanner cin=new Scanner (new BufferedInputStream(System.in));
     9         PrintWriter cout=new PrintWriter(System.out);
    10         int t;
    11         t=cin.nextInt();
    12         int eg=1;
    13         while(t>0)
    14         {
    15             BigInteger eight=new BigInteger("8");
    16             BigInteger seven=new BigInteger("7");
    17             BigInteger one=new BigInteger("1");
    18             String inp;
    19             inp=cin.next();
    20             BigInteger n=new BigInteger(inp);
    21             BigInteger ans;
    22             BigInteger ans2;
    23             ans=n.multiply(n).multiply(eight);
    24             ans2=n.multiply(seven);
    25             ans=ans.subtract(ans2);
    26             ans=ans.add(one);
    27             cout.println("Case #"+eg+": "+ans);
    28             ++eg;
    29             --t;
    30             
    31         }
    32         cout.flush();
    33     }
    34 
    35 }

    大数的所有计算都是与大数之间进行的

    BIgInteger a,b;

    a=a.add(b);

    a=a.substract(b);

    a=a.multiply(b);

    a=a.divied(b);

    a=a.gcd(b);

  • 相关阅读:
    Tensorflow CIFAR10 (二分类)
    2018年阿里巴巴重要开源项目汇总
    环境变量备份
    ubuntu Qt5 librealsense opencv
    ubuntu16.04 qt opencv3.4
    时间作为文件名
    ubuntu16.04 安装opencv3
    Visual studio 2015/2017 opencv3.4 kineck2.0 osg realsense配置
    开源监控系统整合Nagios+Cacti+Nconf详解
    nagios系列(八)之nagios通过nsclient监控windows主机
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/4761854.html
Copyright © 2011-2022 走看看