课堂作业——返回整数数组最大子数组


第一题

 1 package com.Cra2iTeT;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * @author Cra2iTeT
 7  * @version 1.0
 8  * @date 2022/3/11 10:16
 9  */
10 public class test01 {
11     public static void main(String[] args) {
12         Scanner scanner = new Scanner(System.in);
13 
14         int length = scanner.nextInt();
15 
16         int tempInt[] = new int[length];
17 
18         int max = 0;
19         int tempMax = 0;
20 
21         for (int i = 0; i < length; i++) {
22             tempInt[i] = scanner.nextInt();
23 
24             if (i == 0) {
25                 tempMax = tempInt[i];
26                 max = tempInt[i];
27             } else {
28                 if (tempMax < 0) {
29                     tempMax = tempInt[i];
30                 } else {
31                     tempMax += tempInt[i];
32                 }
33             }
34 
35             if (tempMax > max) {
36                 max = tempMax;
37             }
38         }
39 
40         System.out.println(max);
41 
42     }
43 }

第二题

 1 package com.Cra2iTeT;
 2 
 3 import java.io.*;
 4 import java.util.Scanner;
 5 
 6 /**
 7  * @author Cra2iTeT
 8  * @version 1.0
 9  * @date 2022/3/11 11:00
10  */
11 public class test02 {
12     public static void main(String[] args) {
13         try {
14             Scanner scanner = new Scanner(new FileReader("src/com/Cra2iTeT/test02.txt"));
15 
16             int row = 0;
17             int column = 0;
18             int tempRow, tempColumn;
19 
20             for (int i = 0; i < 2 && scanner.hasNextLine(); i++) {
21                 String line = scanner.nextLine();
22                 if (i == 0) {
23                     row = Integer.parseInt(line.substring(0, line.indexOf(',')));
24                 } else {
25                     column = Integer.parseInt(line.substring(0, line.indexOf(',')));
26                 }
27             }
28 
29             if (row <= 0 || column <= 0) {
30                 System.out.println("行数或者列数不对");
31                 return;
32             }
33 
34             int tempInt[] = new int[row * column];
35             int tempIndex = 0;
36 
37             for (tempRow = 0; tempRow < row && scanner.hasNextLine(); tempRow++) {  //不存在下一行或者已经读完该读的行数
38                 String line = scanner.nextLine();
39 
40                 for (tempColumn = 0; tempColumn < column - 1 && !line.equals(""); tempColumn++) {
41                     tempInt[tempIndex++] = Integer.parseInt(line.substring(0, line.indexOf(',')));  //先获取从头开始至第一个逗号之间的数
42                     line = line.substring(line.indexOf(',') + 1, line.length());    //将第一个逗号之后的值覆盖原来的值
43                 }
44 
45                 if (!line.equals("")) { //最后一个数因为没有逗号所以需要单独判断(有些缺陷,万一给的数据错误,正常应该结束的位置没有结束,后面还有数据的话读数据应该会有错,只是猜想,没尝试)
46                     tempInt[tempIndex++] = Integer.parseInt(line.substring(0));
47                     tempColumn++;
48                 }
49 
50                 if (tempColumn != column) {
51                     System.out.println("数据列数不对");
52                     return;
53                 }
54             }
55 
56             if (tempRow != row || scanner.hasNextLine()) {
57                 System.out.println("数据行数不对");
58                 return;
59             }
60 
61             int max = 0;
62             int tempMax = 0;
63 
64             for (int i = 0; i < row * column; i++) {
65                 if (i == 0) {
66                     tempMax = tempInt[i];
67                     max = tempInt[i];
68                 } else {
69                     if (tempMax < 0) {
70                         tempMax = tempInt[i];
71                     } else {
72                         tempMax += tempInt[i];
73                     }
74                 }
75 
76                 if (tempMax > max) {
77                     max = tempMax;
78                 }80             }
81 
82             System.out.println(max);
83 
84         } catch (FileNotFoundException e) {
85             e.printStackTrace();
86         }
87     }
88 }

 

 实际做题时间在十几分钟左右吧,核心还是把二维转化成一维计算,如注释所写的应该是存在一定缺陷的,后续再改。