在之前的基础上又安排了二维数组的,在课上一开始是理解错要求了,简单的以为用循环数组就能解决,但是却忽视了子数组是否能构成矩形,之后课下和同学们讨论,主要是多重遍历,但是我还是没搞明白怎么构成新的二维数组,之后同学给了一段源代码,自己还在摸索:

  1 package b;
2
3 import java.util.Scanner;
4
5 import javax.print.attribute.standard.PrinterLocation;
6
7 public class b
8 {
9 public static void main(String[] args)
10 {
11 Scanner scanner=new Scanner(System.in);
12 System.out.println("请输入二维数组的行和列:");
13 int hang=scanner.nextInt();
14 int lie=scanner.nextInt();
15 System.out.println("请输入二维数组:");
16 int[][] shuzu=new int[hang][lie];
17
18 int i,j;
19 for(i=0;i<hang;i++)
20 {
21 for(j=0;j<lie;j++)
22 {
23 shuzu[i][j]=scanner.nextInt();
24 }
25 }
26
27 System.out.println("此数组中连续和的最大值为:"+max(shuzu,hang,lie));
28
29 }
30
31 static int max(int shuzu[][],int hang,int lie)
32 {
33 int max=0,sum=0;
34 int i,j;
35 int[] b=new int[lie];
36 int[] up=new int[hang];
37 int[] down=new int[hang];
38 int[] t=new int[hang];
39 if(lie==1)
40 {
41 for (i=0;i<hang;i++)
42 {
43 sum+=shuzu[i][0];
44 if(sum<0)
45 {
46 sum=0;
47 }
48 if(sum>max)
49 {
50 max=sum;
51 }
52 }
53
54 if(max==0)
55 {
56 for(i=0;i<hang;i++)
57 {
58 if(i==0)
59 {
60 max=shuzu[i][0];
61 }
62 if(shuzu[i][0]>max)
63 {
64 max=shuzu[i][0];
65 }
66 }
67 }
68 }
69 else
70 {
71 for(i=0;i<hang;i++)
72 {
73 for(j=0;j<lie;j++)
74 {
75 b[j]=shuzu[i][j];
76 }
77 int[] c=new int[100];
78 c[0]=0;
79 int sum1=0,max1=0,k;
80 for(k=0;k<lie;k++)
81 {
82 if(sum1<0)
83 {
84 sum1=b[k];
85 }
86 else
87 {
88 sum1=sum1+b[k];
89 }
90 c[k]=sum1;
91 }
92 max1=c[0];
93 int mmark=0,smark=0;
94 for(k=0;k<lie;k++)
95 {
96 if (max1<c[k])
97 {
98 max1= c[k];
99 mmark = k;
100 }
101 }
102 for (k = mmark;k >= 0;k--)
103 {
104 if (c[k] == b[k])
105 {
106 smark = k;
107 break;
108 }
109 }
110 sum=max1;
111
112 up[i]=smark;
113 down[i]=mmark;
114 t[i]=sum;
115
116 }
117 int t2=t[0];
118 for(i=0;i<hang-1;i++)
119 {
120 if(up[i]<=down[i+1] && down[i]>=up[i+1])
121 {
122 t2+=t[i+1];
123 }
124 for(j=up[i];j<up[i+1];j++)
125 {
126 if(shuzu[i+1][j]>0) t2+=shuzu[i+1][j]; //判别独立正数
127 }
128
129 }
130 max=t2;
131 }
132 return max;
133 }
134 }

求数组的子数组之和的最大值IV的更多相关文章

  1. 求数组的子数组之和的最大值II

    这次在求数组的子数组之和的最大值的条件下又增加了新的约束:  1.要求数组从文件读取.      2.如果输入的数组很大,  并且有很多大的数字,  就会产生比较大的结果 (考虑一下数的溢出), 请保 ...

  2. 求数组的子数组之和的最大值III(循环数组)

    新的要求:一维数组改成循环数组,只是涉及简单算法,只是拿了小数做测试 想法:从文件读取数组,然后新建数组,将文件读取的数组在新数组中做一下连接,成为二倍长度的数组,然后再遍历,将每次遍历的子数组的和存 ...

  3. C#中求数组的子数组之和的最大值

    <编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...

  4. N元数组的子数组之和的最大值

    题目:有N个整数的元素的一维数组,求子数组中元素之和中最大的一组(思想:动态规划) 分析: 设该数组为array[N], 那么对于array[i]该不该在元素之和最大的那个子数组中呢?首先,不如假设a ...

  5. 求二维数组联通子数组和的最大值 (联通涂色) beta!

    算法十分臃肿,效率捉鸡,不知用了多少循环,还有bug...任重道远,编程之美. 思想:按行遍历,找出每行的最大子数组.若行间都联通,行最大子数组相加后,再加上独立的正数.若行间不连通,找出较大子路径, ...

  6. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  7. Minimum Size Subarray Sum 最短子数组之和

    题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...

  8. [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

  9. lintcode:子数组之和为0

    题目: 子数组之和 给定一个整数数组,找到和为零的子数组.你的代码应该返回满足要求的子数组的起始位置和结束位置 样例 给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3]. 解 ...

随机推荐

  1. 机器学习PAL产品优势

    机器学习PAL产品优势 PAI支持丰富的机器学习算法.一站式的机器学习体验.主流的机器学习框架及可视化的建模方式.本文介绍PAI的产品优势. 丰富的机器学习算法 PAI的算法都经过阿里巴巴集团大规模业 ...

  2. TensorRT IRNNv2Layer

    TensorRT IRNNv2Layer IRNNv2Layer层实现递归层,如递归神经网络(RNN).门控递归单元(GRU)和长短期记忆(LSTM).支持的类型有RNN.GRU和LSTM.它执行一个 ...

  3. adb安装 mac和Windows

    一.mac安装 参考地址https://blog.csdn.net/VSRfind/article/details/79593098 1.首先安装一个软件 在用Mac进行Android开发之前,我们一 ...

  4. JUC 并发编程--10, 阻塞队列之--LinkedBlockingDeque 工作窃取, 代码演示

    直接上代码 class LinkedBlockingDequeDemo { // 循环是否结束的开关 private static volatile boolean flag1 = true; pri ...

  5. Qt项目ui文件中新添加的控件在代码中不识别的问题解决

    今天在学Qt框架的信号槽,然后发现在ui中加的控件,通过ui-> 找不到,没有识别,于是上网查找了一下问题 解决方法 添加ui控件后,执行程序,退出程序,将debug目录下的ui_XXXX.h拷 ...

  6. JSON.parse无双引号如何实现转换

    用JSON.parse()做转换,必须用双引号包起来.但是我用chrome的devtools时,它可以自动转换.于是上网查了一下,原来可以通过replace方法格式化一下.(修改后可以兼容日期格式) ...

  7. SpringBoot数据访问(二) SpringBoot整合JPA

    JPA简介 Spring Data JPA是Spring Data大家族的一部分,它可以轻松实现基于JPA的存储库.该模块用于增强支持基于JPA的数据访问层,它使我们可以更加容易地构建使用数据访问技术 ...

  8. springcloud webflux

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  9. DG duplicate报错:RMAN-05001:auxiliary file name /u01/app/oracle/oradata/fratbs01.dbf conflicts with a file used by the target database

    问题:rman duplicate时报错: RMAN-05001:auxiliary file name /u01/app/oracle/oradata/fratbs01.dbf conflicts ...

  10. HTTP头部POST表单详解

    2 POST /hello/checkUser.html?opt=xxx HTTP/1.1 方法的声明,Get,Post,Delete等 3 Accept: */* 4 Referer: http:/ ...