zoukankan      html  css  js  c++  java
  • 2020022802

    思路就是很简单的

    遍历以每个数字开头时的所有子集合,然后比较,记下最大值

    时间复杂度为O(n^2)

     1 package hmk0;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Scanner;
     5 
     6 class TClass {
     7     public int value;
     8 
     9     public TClass() {
    10         value = 0;
    11     }
    12 
    13     public TClass(int i) {
    14         value = i;
    15     }
    16 }
    17 
    18 class Tmp {
    19     public static int func(ArrayList<TClass> ipt, int length) {
    20         int i = 0;
    21         int j = 0;
    22         int tmp = 0;
    23         int rtn = ipt.get(0).value;// 初始化防止全负
    24         boolean flag = true;// 标记环状回到开头
    25         for (i = 0; i < length; i++) {
    26             tmp = 0;
    27             flag = true;
    28             for (j = i; j < i || flag; j++) {
    29                 if (j == length) {
    30                     if (i == 0)
    31                         break;
    32                     j = 0;
    33                     flag = false;
    34                 }
    35                 tmp = tmp + ipt.get(j).value;
    36                 if (tmp > rtn) {
    37                     rtn = tmp;
    38                 }
    39             }
    40         }
    41         return rtn;
    42     }
    43 
    44     public static void main(String args[]) {
    45         int l = 0;
    46         int i = 0;
    47         int tmp = 0;
    48         ArrayList<TClass> ipt = new ArrayList<TClass>();
    49         Scanner sipt = new Scanner(System.in);
    50         System.out.println("Enter The Length");
    51         l = sipt.nextInt();
    52         if (sipt.hasNextLine()) {
    53             sipt.nextLine();
    54         }
    55         for (i = 0; i < l; i++) {
    56             tmp = sipt.nextInt();
    57             if (sipt.hasNextLine()) {
    58                 sipt.nextLine();
    59             }
    60             ipt.add(new TClass(tmp));
    61         }
    62         System.out.println("Output:" + func(ipt, l));
    63     }
    64 }
  • 相关阅读:
    Mac OSX 读写usb composite设备
    std io的一个笔记
    国庆假期掠影
    c++类型转化
    operator new and delete
    一个递归求和的两种方法
    10.24,今天是程序员节
    基于MyUsbDevice类的一个例子
    逆波兰表达式笔记
    2012年的最后一天
  • 原文地址:https://www.cnblogs.com/minadukirinno/p/13073657.html
Copyright © 2011-2022 走看看