个人作业一
课堂练习——返回一个整数数组中最大的子数组的和
第一阶段:

主要难点在于如何把时间复杂度控制在O(n),当时想了很久都没想明白,这是听了同学的思路之后按照他的思路进行编程的。整体来说难点不大,具体代码 如下:
import java.util.Scanner;
/**
* @author migua
* @version 1.0
* @date 2022/3/11 9:44
*/
public class aa {
public static void main(String[] args) {
System.out.println("数组长度");
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int[] arrNum = new int[l];
System.out.println("数组的值");
for (int i = 0; i < l; i++) {
int x = sc.nextInt();
if(x < 2147483647 || x > -2147483648 )
arrNum[i] = x;
else
{
System.out.println("数据输入超界,程序已退出");
return;
}
//System.out.println(arrNum[i]);
}
int[] arrRes = new int[l];
for(int i = 0;i<arrNum.length;i++)
{
arrRes[i]=0;
}
int i = 0,j=0;
int max = 0;
for (i = 0; i < l; i++) {
//j为当前子数组的开头下标
if(i == 0)
{
arrRes[i]=arrNum[0];
//System.out.println("初始arrRes值为:" + arrRes[i]);
}
else if(i > 0) {
int pre = arrRes[i - 1];
int now = arrNum[i] + arrRes[i - 1];
if(pre < 0)
{
arrRes[i]=arrNum[i];
}
else if (pre < now) {
arrRes[i] = now;
//System.out.println("arrRes[i]" + arrRes[i]);
}
else {
j=i;
arrRes[i] = arrNum[i];
}
}
}
max=arrRes[0];
for( i = 0;i <arrRes.length;i++)
{
if(arrRes[i]>max && arrRes[i] <2147483647)
max=arrRes[i];
else if(arrRes[i] >= 2147483647 || arrRes[i] < -2147483648) {
System.out.println("数据溢出,程序已退出");
return ;
}
}
System.out.println(max);
}
}
第二阶段:

注意点:
(1)通过Scanner、相对路径来读取文件
(2)scanner.hasNextLine()函数读取下边对应行的内容
(3) 对应较大的数据时,简单的判断会非常地麻烦。从朝勇那里听到BigDecimal,然后就去学了一下,真的好用,所有问题直接迎刃而解。
代码如下:
/**
* @author migua
* @version 1.0
* @date 2022/3/11 19:25
*/
import java.io.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;
public class bb {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new FileReader("src/test.txt"));
int row = 0;
int column = 0;
int tempRow, tempColumn;
for (int i = 0; i < 2 && scanner.hasNextLine(); i++)
{
String L = scanner.nextLine();
if (i == 0)
{
row = Integer.parseInt(L.substring(0, L.indexOf(',')));
}
else
{
column = Integer.parseInt(L.substring(0, L.indexOf(',')));
}
}
if (row <= 0 || column <= 0)
{
System.out.println("行数或者列数出错");
return;
}
int tempInt[] = new int[row * column];
int tempIndex = 0;
for(tempRow =0;tempRow<row &&scanner.hasNextLine();tempRow++)
{ //不存在下一行或者已经读完该读的行
String L = scanner.nextLine();
for (tempColumn = 0; tempColumn < column - 1 && !L.equals(""); tempColumn++)
{
tempInt[tempIndex++] = Integer.parseInt(L.substring(0, L.indexOf(','))); //先获取从头开始至第一个逗号之间的数
L = L.substring(L.indexOf(',') + 1, L.length()); //将第一个逗号之后的值覆盖原来的值
}
if (!L.equals(""))
{
tempInt[tempIndex++] = Integer.parseInt(L.substring(0));
tempColumn++;
}
if (tempColumn != column)
{
System.out.println("数据的列数不对");
return;
}
}
if (tempRow != row || scanner.hasNextLine()) {
System.out.println("数据的行数不对");
return;
}
BigDecimal max = new BigDecimal("0");
BigDecimal tempMax = new BigDecimal("0");
for (int i = 0; i < row * column; i++)
{
if (i == 0)
{
BigDecimal temp = new BigDecimal(tempInt[i]);
tempMax=tempMax.add(temp);
max=max.add(temp);
//max = tempInt[i];
}
else
{
if (tempMax.compareTo(new BigDecimal("0")) == -1) //tempMax < 0
{
tempMax = new BigDecimal(tempInt[i]);
}
else
{
tempMax=tempMax.add(new BigDecimal(tempInt[i]));
}
}
if (tempMax.compareTo(max) == 1) //tempMax > max
{
max = tempMax;
}
}
System.out.println(max);
} catch(FileNotFoundException e){
e.printStackTrace();
}
}
}


浙公网安备 33010602011771号