求数组的子数组之和的最大值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]. 解 ...
随机推荐
- vue中dom节点转图片
今天项目中有个需求,需要将页面的一部分转为图片,相当于存证之类的 然后我查阅了下百度,发现了几种可行的方法,特此来记录下 1.html2canvas插件 安装:npm install --save h ...
- Java重载方法如何竞争
突然想起以前遇到的一道笔试题,题目大概是这样子的 // 父类 public class Father { } // 子类 public class Son extends Father { } // ...
- mybatis——解决属性名和数据库字段名不一致问题(注解方式)
当我们使用注解开发时有时会遇到数据库字段名与实体类属性名不一致的问题.xml方式开发可以通过结果集映射的方式解决,那注解方式开发要怎么解决呢? 注解解决方式: @Results()注解 Results ...
- 2.docker下centos镜像
1.下载并运行 # 交互模式下载并运行centos容器 $ docker run -it centos:latest /bin/bash 1.1 配置centos的环境别名 $ vi /etc/bas ...
- Typora 配置码云图床
目录 在码云创建一个项目作为自己床图 设置私人令牌 下载安装 PigGo Typora中设置图片上传选项 在码云创建一个项目作为自己床图 创建的项目必须为公开项目,创建的过程不细说了. 设置私人令牌 ...
- Zab协议 (史上最全)
文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...
- csp-s模拟测试42「世界线·时间机器·密码」
$t3$不会 世界线 题解 题目让求的就是每个点能到点的数量$-$出度 设每个点能到的点为$f[x]$ 则$f[x]=x \sum\limits_{y}^{y\in son[x]} U f[y]$ 用 ...
- WPF使用 INotifyPropertyChanged 实现数据驱动
如下图,有这么一个常见需求,在修改表单明细的苹果价格时,总价会改变,同时单据总和也随之改变. 按照Winfrom事件驱动的思想来做的话,我们就需要在将UI的修改函数绑定到CellEdit事件中来实现. ...
- 学习Qt Charts - 实时曲线
1.添加坐标轴 按照之前的一篇文章,先在工程中添加QChart.QChartView,代码如下: Dialog::Dialog(QWidget *parent) : QDialog(parent), ...
- zabbix4.0升级到zabbix5.0
1 更新yum源 # yum erase zabbix-release-4.0-1.el7.noarch # rpm -ivh https://mirrors.aliyun.com/zabbix/za ...