zoukankan      html  css  js  c++  java
  • Java作业6

    1,利用二维数组和循环语句制作一个五子棋盘

    2.编写一个程序将整数转化为汉字

     

    3大数

    四.大数

    4.

    前面几讲介绍过JDK所提供的BigInteger能完成大数计算,如果不用它,直接使用数组表达大数,你能实现相同的功能吗? 要求: (1)用你的大数类实现加和减两个功能 (2)阅读BigInteger类源码,弄清楚它是使用什么算法实现加减乘除四种运算的? (3)通过互联网查找大数运算的相关资料,给你的大数类添加乘、除、求阶乘等其它功能。

    (1)BigInteger历史介绍
    在java中,存在很多种类的数据类型,例如byte short char int float double long,而BigInteger属于其中一个比较特殊的数据类型,也是本教程关注的重点。BigInteger在JDK1.1中就已经存在了,属于java.math包的类。从名字来看,BigInteger比Integer表示数值的范围更大一些。BigInteger类的基本结构如下所示:
    java.lang.Object
    |_java.lang.Number
    |_java.math.BigInteger
    BigInteger已实现的接口:Serializable, Comparable<BigInteger>

    (2)BigInteger是不可变的任意精度的整数。所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型)。BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。

    (3)BigInteger属性分析
    下面看看BigInteger有哪些重点的属性,主要的有下面三个:
    (1)final int signum
    signum属性是为了区分:正负数和0的标志位,JDK注释里面已经说的很明白了:
    The signum of this BigInteger: -1 for negative, 0 for zero, or 1 for positive. Note that the BigInteger zero must have a signum of 0. This is necessary to ensures that there is exactly one representation for each BigInteger value.
    (2)final int[] mag
    mag是magnitude的缩写形式,mag数组是存储BigInteger数值大小的,采用big-endian的顺序,也就是高位字节存入低地址,低位字节存入高地址,依次排列的方式。JDK原文注释如下:
    The magnitude of this BigInteger, in big-endian order: the zeroth element of this array is the most-significant int of the magnitude. The magnitude must be "minimal" in that the most-significant int (mag[0]) must be non-zero. This is necessary to ensure that there is exactly one representation for each BigInteger value. Note that this implies that the BigInteger zero has a zero-length mag array.
    (3)final static long LONG_MASK = 0xffffffffL;
    This mask is used to obtain the value of an int as if it were unsigned。

    package Work;
    
    import java.util.Scanner;
    
    public class BigNum {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
          
          int aa,bb;
          System.out.println("用数组实现大数的加法和减法");
          System.out.print("请输入大数a:");
          Scanner scan=new Scanner(System.in);
          String a=scan.next();
          System.out.print("请输入大数b:");
         String b=scan.next();
         int A[]=new int[100];
         int B[]=new int[100];
         
         for(int i=0;i<a.length();i++){
             A[i]=(int) ((a.charAt(i)-48)*Math.pow(10,a.length()-i-1));
         }
         for(int i=0;i<b.length();i++){
             B[i]=(int) ((b.charAt(i)-48)*Math.pow(10,b.length()-i-1));
         }
         int sum=0;
         int sub=0;
         for(int i=0;i<a.length();i++){
             sum+=A[i]+B[i];
             sub+=A[i]-B[i];
         }
         System.out.print("a+b="+sum);
          System.out.println();
          System.out.print("a-b="+sub);
        }
    
    }

    结果解截图:

    4随机数

     

     

    for循环在函数中生成十个随机数,放入数组,放入sum涌入累加和。

  • 相关阅读:
    危机下,你还敢提加薪吗?
    大白兔奶糖三聚氰胺事件后21日起重新上架
    15个nosql数据库
    向网页设计师推荐15个很棒的网站
    腾讯新浪通过IP地址获取当前地理位置(省份)的接口
    5个最顶级jQuery图表类库插件Charting plugin
    12种JavaScript MVC框架之比较
    企业网站设计的启示
    游戏引擎大全
    推荐几份能够帮助你学习 CSS3 的实用帮助手册
  • 原文地址:https://www.cnblogs.com/zeminzhang/p/6036443.html
Copyright © 2011-2022 走看看