求数组的子数组之和的最大值III(循环数组)
新的要求:一维数组改成循环数组,只是涉及简单算法,只是拿了小数做测试
想法:从文件读取数组,然后新建数组,将文件读取的数组在新数组中做一下连接,成为二倍长度的数组,然后再遍历,将每次遍历的子数组的和存到result数组中,最后比较result数组的最大值
代码如下:
1 package test2;
2 import java.io.BufferedReader;
3 import java.io.File;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Scanner;
8
9 public class shuzu2 {
10
11 static Scanner cin = new Scanner(System.in);
12
13 public static void readFileByLines(String fileName) {
14 File file = new File(fileName);
15 BufferedReader reader = null;
16 try {
17 reader = new BufferedReader(new FileReader(file));
18 String tempString = null;
19
20 while ((tempString = reader.readLine()) != null) {
21
22 System.out.println(tempString);
23
24 }
25 reader.close();
26 } catch (IOException e) {
27 e.printStackTrace();
28 } finally {
29 if (reader != null) {
30 try {
31 reader.close();
32 } catch (IOException e1) {
33 }
34 }
35 }
36 }
37
38 public static long[] toArrayByFileReader1(String name) {
39 // 使用ArrayList来存储每行读取到的字符串
40 ArrayList<String> arrayList = new ArrayList<>();
41 try {
42 FileReader fr = new FileReader(name);
43 BufferedReader bf = new BufferedReader(fr);
44 String str;
45 // 按行读取字符串
46 while ((str = bf.readLine()) != null) {
47 arrayList.add(str);
48 }
49 bf.close();
50 fr.close();
51 } catch (IOException e) {
52 e.printStackTrace();
53 }
54 // 对ArrayList中存储的字符串进行处理
55 int length = arrayList.size();
56 long[] array = new long[length];
57 for (int i = 0; i < length; i++) {
58 String s = arrayList.get(i);
59 array[i] = Long.parseLong(s);
60 }
61 long[] newarray = new long[5*length];//剪开带子后新数组
62 long[] result = new long[1000];//存放求和子数组
63 int rs=0;//子数组计数器
64 long f=0;//定义整形变量f,为子数组最大值
65 long sum=0;//定义整形变量sum,为子数组求和
66 System.out.println("输入遍历开始的位置:");
67 int a = cin.nextInt();
68 int sa = 0;//a用来存储剪开带子位置
69 int k = 0 ;
70 long []jieguo = new long[1000];
71 for(int j = 0;j < array.length;j++)
72 {
73 newarray[k++] = array[j];
74 newarray[j+array.length] = array[j];
75 //System.out.println("1 "+newarray[j]);
76 }
77 for(int i=0;i<2*array.length;i++)
78 System.out.println("2 "+newarray[i]);
79 int mlist,slist=0;
80 long max;
81 for(int i=0;i<length;i++) //O(n^2)求子数组之和
82 {
83 mlist = 0;
84 for(int j=i;j<length+i;j++)
85 {
86 mlist +=newarray[j];
87 result[rs++] = mlist; //将子数组之和存在数组result[]内
88 System.out.println("第"+ (slist+1) +"个子数组的和为:" + mlist);
89 slist++;
90 }
91 }
92 for(int i=0;i<rs;i++)
93 {System.out.println("reslut"+i+" "+result[i]);}
94 max = result[0]; //将子数组和数组第一个值给max,然后循环进行比较,求出最大值
95 for(int i = 0;i<slist;i++)
96 {
97 if(max < result[i])
98 max = result[i];
99 }
100 //将新数组存一下
101 System.out.println("该数组的子数组之和的最大值为:"+max);
102 // 返回数组
103 return array;
104 }
105
106
107 public static void main(String[] args) throws IOException{
108
109 String name = new String("E:\\Program Files\\eclipse操作\\shuzu\\src\\test2\\input.txt");
110
111 readFileByLines(name);
112 toArrayByFileReader1(name);//文件路径
113
114 }
115 }
运行结果:


遇到的问题:在这次处理中,数组下标越界的情况比较多,考虑情况比较少,课上想用的是数据结构中学的循环队列来解决,但是没有实现,算法还得好好复习复习。
求数组的子数组之和的最大值III(循环数组)的更多相关文章
- 求数组的子数组之和的最大值II
这次在求数组的子数组之和的最大值的条件下又增加了新的约束: 1.要求数组从文件读取. 2.如果输入的数组很大, 并且有很多大的数字, 就会产生比较大的结果 (考虑一下数的溢出), 请保 ...
- C#中求数组的子数组之和的最大值
<编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...
- 求数组的子数组之和的最大值IV
在之前的基础上又安排了二维数组的,在课上一开始是理解错要求了,简单的以为用循环数组就能解决,但是却忽视了子数组是否能构成矩形,之后课下和同学们讨论,主要是多重遍历,但是我还是没搞明白怎么构成新的二维数 ...
- N元数组的子数组之和的最大值
题目:有N个整数的元素的一维数组,求子数组中元素之和中最大的一组(思想:动态规划) 分析: 设该数组为array[N], 那么对于array[i]该不该在元素之和最大的那个子数组中呢?首先,不如假设a ...
- 求二维数组联通子数组和的最大值 (联通涂色) beta!
算法十分臃肿,效率捉鸡,不知用了多少循环,还有bug...任重道远,编程之美. 思想:按行遍历,找出每行的最大子数组.若行间都联通,行最大子数组相加后,再加上独立的正数.若行间不连通,找出较大子路径, ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- Minimum Size Subarray Sum 最短子数组之和
题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...
- [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 ...
- lintcode:子数组之和为0
题目: 子数组之和 给定一个整数数组,找到和为零的子数组.你的代码应该返回满足要求的子数组的起始位置和结束位置 样例 给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3]. 解 ...
随机推荐
- Ascend Pytorch算子适配层开发
Ascend Pytorch算子适配层开发 适配方法 找到和PyTorch算子功能对应的NPU TBE算子,根据算子功能计算出输出Tensor的size,再根据TBE算子原型构造对应的input/ou ...
- 如何为应用选择最佳的FPGA(下)
如何为应用选择最佳的FPGA(下) How to select an FPGA board? FPGA板的选择在很大程度上受FPGA本身的影响,也受整个板的特性和性能的影响.们已经在上面的章节中讨论了 ...
- LeetCode---105. 从前序与中序遍历序列构造二叉树 (Medium)
题目:105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7 ...
- 使用JS获取两个时间差(JS写一个倒计时功能)
<body onload="myFunction()"> <p id="demo"></p> <script> ...
- Solon Auth 认证框架使用演示(更简单的认证框架)
最近看了好几个认证框架,什么 Appache Shiro 啦.Sa-Token 啦.Spring Security啦...尤其是Spring Security,做为对标 Spring Boot &am ...
- Spring Cloud Data Flow整合Cloudfoundry UAA服务做权限控制
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 关于Spring Cloud Data Flow这里不多介绍,有兴趣可以看下面的文章.本文主要介绍如何整合Dat ...
- 无需会员将有道云笔记脑图转换xmind
我的烦恼 有道云笔记有脑图功能,我平时经常用到,之所以很少用到其他脑图工具,是因为我一直用有道云笔记写笔记.因此编辑脑图和查看脑图比较方便,但是需要将脑图导出的时候目前只支持图片和xmind,但是需要 ...
- some requirement checks failed
1.执行安装数据库软件时报错(./runInstaller): 解决:(1)su - root 执行: x host+ 然后 su - oracle 执行:./runIstal ...
- Linux常见信号介绍
1.信号 首先信号我们要和信号量区分开来,虽然两者都是操作系统进程通信的方式.可以简单的理解,信号是用来通知进程发生了什么需要做什么,信号量一般是用作进程同步(pv操作) 2.常见信号量 (以下数字标 ...
- 555定时器(1)单稳态触发器电路及Multisim实例仿真
555定时器(Timer)因内部有3个5K欧姆分压电阻而得名,是一种多用途的模数混合集成电路,它能方便地组成施密特触发器.单稳态触发器与多谐振荡器,而且成本低,性能可靠,在各种领域获得了广泛的应用. ...