zoukankan      html  css  js  c++  java
  • 软件工程第二周个人作业

    package text;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class three {
        
        public static int length=0;
        
        public static int[] readArray() {
            int[] a = new int[100];
            int t;
            String num="";
            try {
                BufferedReader br = new BufferedReader(new FileReader("C:\Users\AZ\Desktop\Array.txt"));
                while ((t = br.read()) != -1) {
                    if(t==45 && num=="") {    
                        num="-";
                        continue;
                    }
                    if(t>=48 && t<=57)   
                        num += (char)t;
                    if((t<48 || t>57) && num!="" && num!="-") {  
                        a[length++]=Integer.parseInt(num);
                        if(t==45)        
                            num="-";
                        else
                            num="";
                    }
                }
                if(!(num=="" || num=="-"))  
                    a[length++]=Integer.parseInt(num);            
                br.close();
            } catch (FileNotFoundException e) {
                System.out.println("文件不存在!");
            } catch (IOException e) {
                System.out.println("文件读取异常!");
            } 
            return a;
        }
        
        public static void maxnumber(int[] a) {
            int[] b = new int[100]; 
            b[0] = a[0];
            int j=0,max=0,maxnum=0;
            for(int i=1;i<length;i++) {
                if(a[i]<0 && b[j]>0)
                    b[++j] = a[i];
                else if(a[i]>0 && b[j]<0)
                    b[++j] = a[i];
                else if(a[i]<=0 && b[j]<0)
                    b[j] += a[i];
                else if(a[i]>=0 && b[j]>0)
                    b[j] += a[i];
            }
           
            if(b[0]<=0 && b[1]==0) {    
                max=a[0];
                for(int i=1;i<10;i++)
                    if(max<a[i])
                        max=a[i];
            } 
            else {
                max=b[0];
                for(int i=0;i<=j;i++)
                    if(max<b[i]) {
                        max=b[i];
                        maxnum=i;
                    }
                int t=0;
                for(int i=maxnum;i<=j-2;i+=2) {    
                    t=t+b[i+1]+b[i+2];        
                    if(t>0) {
                        max+=t;
                        t=0;
                    }
                }
                t=0;
                for(int i=maxnum;i>=2;i-=2) {   
                    t=t+b[i-1]+b[i-2];        
                    if(t>0) {
                        max+=t;
                        t=0;
                    }
                }
            }    
            System.out.println("最大子数组的和为:" + max);
        }
        public static void main(String[] args) {
            maxnumber(readArray());
        }
    }

    能做到读取文件,不过程序的健壮性不敢保证。

  • 相关阅读:
    【读书笔记】简约至上交互设计四策略目录
    Cassandra在Windows上安装及使用方法[转]
    [转]揭秘全球最大网站Facebook背后的那些软件
    过程改进计划
    制定项目管理计划
    在sublime text3中利用markdown
    ubuntu下更改用户名和主机名
    国庆有感
    最近两天学到的技术汇总
    看见了就当没有看见
  • 原文地址:https://www.cnblogs.com/vvxvv/p/12378200.html
Copyright © 2011-2022 走看看