• 软件工程概论作业04


    题目:返回一个整数数组中最大子数组的和。

    要求: 输入一个一维整形数组,数组里有正数也有负数。 一维数组首尾相接,象个一条首尾相接带子一样。 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。 发表一篇博客文章讲述设计思想,出现的问题,可能的解决方案(多选)、源代码、结果截图、总结。

     1 package demo;  
     2   
     3 public class T1 {  
     4     public static int Max(int[] a) {  
     5         int []b=new int[10];
     6         int m = 0;
     7         int n = 0;  
     8         int i;
     9         int j=0;
    10         int t=0;
    11         for (i = 0; i < a.length; i ++) {  
    12             n += a[i];  
    13             if (n > m)           
    14                 m = n;  
    15             else if (n < 0)  
    16                 n = 0;  
    17         }  
    18         int k=a[0];
    19         for(i = 0;i < a.length;i ++) {            
    20                if(k > a[i]) { 
    21                  k = a[i]; 
    22                  j = i; 
    23                } 
    24         }
    25         t = j; 
    26         i = 0; 
    27         while(j < a.length) {        
    28                 b[i] = a[j]; 
    29                 i ++; 
    30                 j ++;  
    31         }    
    32         j = 0; 
    33         while(j < t) {          
    34                b[i] = a[j];
    35                i ++; 
    36                j ++; 
    37         } 
    38         n = 0; 
    39         for(i = 0;i < a.length;i ++) {          
    40                n += b[i];
    41                if(m < n) {  
    42                   m = n; 
    43                } 
    44                if(n < 0) {               
    45                    n = 0; 
    46                } 
    47         }
    48         return m;
    49  
    50 } 
    51 
    52     public static void main(String[] args) {  
    53         int a[] = { 6 , -5 , 1 , 4, 7, 10};  
    54         System.out.println("最大子数组的和为"+Max(a));  
    55     }  
    56 }
    57   
    58 
    59 
    60 
    61 
    62 
    63 
    64 
    65 
    66 
    67 
    68    
    69    
    70    
    71    
    72    
  • 相关阅读:
    linux 杂类
    set
    C++ 基础 杂类
    linux 添加samba账户
    git 常用命令
    git 设置bitbucket 邮箱、用户
    C++ shared_ptr
    git 免密码配置
    2014的新目标
    为/Date(1332919782070)/转时间2013-09-23
  • 原文地址:https://www.cnblogs.com/jiandanqinxin/p/6666687.html
Copyright © 2020-2023  润新知