zoukankan      html  css  js  c++  java
  • 课堂小测续篇(用文件读取数组求子集之和最大值)

     

     代码:

    package 文件读取子数组最大值;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.math.BigInteger;
    
    
    public class Max {
    
        public static void main(String[] args) throws FileNotFoundException {
        BufferedReader read = new BufferedReader(new FileReader("G:\workspace\Text1.txt"));
        String s;
        BigInteger max = new BigInteger("0");
        BigInteger ans = new BigInteger("0");
        BigInteger n  = new BigInteger("0");
        BigInteger[] a = new BigInteger[100];
        int i = 0;
        try {
            while((s = read.readLine()) != null) {
                try {
                    BigInteger n1 = BigInteger.valueOf(Long.parseLong(s));
                     n = n1;
                    // System.out.println(n+";;;");
                }catch(NumberFormatException e) {
                    System.err.println("输入之中包含非法字符!");
                    break;
                }
              ans = ans.add(n);
              //System.out.println(ans);
               if(ans.compareTo(n)>=0) {// 1 -2 3
                a[i] = ans;
                   if(max.compareTo(a[i])<=0) {
                          max = a[i];
                         // System.out.println(a[i]+"..");
                          }
               }else {
                   ans = n;
                   a[i]= n;
                   if(max.compareTo(a[i])<=0) {
                          max = a[i];
                         // System.out.println(a[i]+"....");
                          }
               }  
               i++;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        System.out.println(max);
        }
    }

    截图:

     

     

     接替思路:为防止溢出,采用BigInteger大数据类型的变量进行操作,将文件中数都进去之后,将数进行加法操作,如果和大于当前数值,就将和储存到数组中,小于则将当前数值存储到数组中,在和ans 的值变为当前数值的值。最后将数组之中的所存数据进行比较,求最大值。

    下面再附着一些大数据运算的具体方法。

    方法名称说明
    add(BigInteger val) 做加法运算
    subtract(BigInteger val) 做减法运算
    multiply(BigInteger val) 做乘法运算
    divide(BigInteger val) 做除法运算
    remainder(BigInteger val) 做取余数运算
    divideAndRemainder(BigInteger val) 做除法运算,返回数组的第一个值为商,第二个值为余数
    pow(int exponent) 做参数的 exponent 次方运算
    negate() 取相反数
    shiftLeft(int n) 将数字左移 n 位,如果 n 为负数,则做右移操作
    shiftRight(int n) 将数字右移 n 位,如果 n 为负数,则做左移操作
    and(BigInteger val) 做与运算
    or(BigInteger val) 做或运算
    compareTo(BigInteger val) 做数字的比较运算
    equals(Object obj) 当参数 obj 是 Biglnteger 类型的数字并且数值相等时返回 true, 其他返回 false
    min(BigInteger val) 返回较小的数值
    max(BigInteger val) 返回较大的数值
  • 相关阅读:
    org.springframework.context.event.AbstractApplicationEventMulticaster
    40. Testing Prev Part IV. Spring Boot features
    nginx+redis 实现 jsp页面缓存,提升系统吞吐率
    SpringDataRedis事务 专题
    cat监控平台环境搭建 专题
    Archive for required library: ‘WebContent/WEB-INF/lib/xxx.jar cannot&n
    m2eclipse简单使用,创建Maven项目 ,运行mvn命令
    如何修改maven默认仓库(即repository)的路径
    MyEclipse2014不支持jre1.8吗
    MyEclipse 2015优化技巧
  • 原文地址:https://www.cnblogs.com/yangxionghao/p/12368037.html
Copyright © 2011-2022 走看看