• 数组


    要求:
    输入一个整形数组,数组里有正数也有负数。
    数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。
    求所有子数组的和的最大值。

    源代码:

     1 package shuzu;
     2 
     3 public class Shuzu {
     4     public static void main(String[] args) {
     5         int nshu;//循环的次数
     6         int shu[] = {-1,3,-4,-3,-2}; 
     7         int max = shu[0];//存储最大的和
     8         for(nshu=0;nshu<5;nshu++) {
     9             int n1 = 0;
    10             int n2 = 0;
    11             for(int nnshu=nshu;nnshu<5;nnshu++) {
    12                 n1 = n1 + shu[nnshu];
    13                 if(nnshu<4) {
    14                     nnshu = nnshu + 1;
    15                     n2 = n1 + shu[nnshu];
    16                     max = maxxx(n1,n2,max);
    17                     nnshu = nnshu - 1;
    18                 }else {
    19                     max = maxx(n1,max);
    20                 }
    21             }
    22         }
    23         System.out.println("最大值" + max);
    24     }
    25     
    26     static int maxxx(int a,int b,int ab) {
    27         int max;
    28         if(a<b) {
    29             max = b;
    30             if (max<ab) {
    31                 max = ab;
    32             }
    33         }else {
    34             max = a;
    35             if(max<ab) {
    36                 max = ab;
    37             }
    38         }
    39         return max;
    40     }
    41     
    42     static int maxx(int a , int b){
    43         int max;
    44         if(a<b) {
    45             max = b;
    46         }else {
    47             max = a;
    48         }
    49         return max;
    50     }
    51     
    52     
    53 }
  • 相关阅读:
    ios数据存储方式FMDB
    ios中的coredata
    编程岗位电话面试问答Top 50[转]
    活动图
    时序图
    用例图
    类图
    Movie
    EasyMock
    cglib应用
  • 原文地址:https://www.cnblogs.com/mawangwang/p/10507796.html
Copyright © 2020-2023  润新知