zoukankan      html  css  js  c++  java
  • 最大子数组求解

    输入一个一维整形数组,数组里有正数也有负数。

    一维数组首尾相接,像个一条首尾相接带子一样。

    数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。

    发表一篇博客文章讲述设计思想,出现的问题,可能的解决方案(多选)、源代码、结果截图、总结。

    public class MaxIntArray {
        public static void main(String [] args){
            int a[]={1,4,5,6,2};
            int Max[]=new int[5];
            Max[0] = 0;
            int i = 0;//数组下标
            int j = 0;//最大值数组下标
            int temp=0;//中间变量
            while(i<a.length){                
                    if(temp+a[i]>=Max[j])
                    {
                        temp=temp+a[i];
                        Max[j]=temp;
                        i++;
                    }
                    else if(temp+a[i]<Max[j]&&temp+a[i]>0)
                    {
                        temp=temp+a[i];
                        i++;
                    }
                    else if(temp+a[i]<=0)
                    {
                       i++;    
                       j++;
                       Max[j]=0;
                       temp=0;
                    }
                
            }
            int max = Max[0];
            for(int k=0;k<=j;k++){
                if(Max[k]>max)
                {
                    max=Max[k];
                }
            }
            /*System.out.println(j);
            for(int k=0;k<=j;k++){
                System.out.println(Max[k]);
            }*/
            System.out.println("最大子数组和为"+max);
        }
    }
  • 相关阅读:
    joblib to implement multi-progress
    python排序
    回归评估:R2
    评估曲线
    模型融合:Stacking
    排列重要性
    单点登陆
    特征漂移检测
    python数组切片
    docker -ti
  • 原文地址:https://www.cnblogs.com/zhanghengstdu/p/6798775.html
Copyright © 2011-2022 走看看