zoukankan      html  css  js  c++  java
  • 一维数组续

    import java.util.Scanner;
    public class yiweishuzu {   
        public static void main(String[] args) {        
            // TODO Auto-generated method stub        
            System.out.println("输入你需要的个数:");       
            Scanner scan = new Scanner(System.in);        
            int n=scan.nextInt();//定义需要的阶层数n  
            int[]array=new int[n];//定义一个n*m的数组array       
            System.out.println("输入数组的各个元素:");        
            for(int i=0;i<n;i++){            
                    
                    array[i]=scan.nextInt();//给数组赋值            
                          
                }        
            System.out.println("你输入的数组为:");        
            for(int i=0;i<n;i++){          
            	 System.out.println(+array[i]); 
                    
                }   //数组显示
           
         int sum=array[1],c=1;
            for(int i=0;i<n;i++) {
            	 int sum1=0;
                for(int j=i;j<n;j++) {
                	if(c>0) {
                            sum1=sum1+array[j];
                            System.out.println("当前为第"+c+"个子数组,子数组和为"+sum1);
                            c++;
                             int a=i+1;
                        int b=j+1; 
                        System.out.println("元素为"+a+"到"+b);
                        }
                        if(sum1>=sum) {
                        sum=sum1;
                       int d=i+1;
                       int e=j+1;
                        System.out.println("当前最大数组和为"+sum);
                        System.out.println("元素为"+d+"到"+e);
                        }
                        
                       
                        
                       
                        }
                }
        System.out.println("最大子数组为:"+sum);
        }   
        }
    

      

  • 相关阅读:
    UOJ388 [UNR #3] 配对树
    洛谷P6151 [集训队作业2019] 青春猪头少年不会梦到兔女郎学姐
    CF908H New Year and Boolean Bridges
    CF704C Black Widow
    CF1338
    CF1361
    CF1110H Modest Substrings
    CF1322
    CF1182F Maximum Sine
    Elastic Stack(ELK)
  • 原文地址:https://www.cnblogs.com/gkl20173667/p/10769805.html
Copyright © 2011-2022 走看看