用Java写一段代码,输出n*n数组所在行和列的和的最大值
时间: 2023-05-22 14:02:18 浏览: 57
以下是Java代码:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
int maxRowSum = Integer.MIN_VALUE;
int maxColSum = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int rowSum = 0;
int colSum = 0;
for (int j = 0; j < n; j++) {
rowSum += arr[i][j];
colSum += arr[j][i];
}
maxRowSum = Math.max(maxRowSum, rowSum);
maxColSum = Math.max(maxColSum, colSum);
}
System.out.println(Math.max(maxRowSum, maxColSum));
}
}
```
输入格式为:
第一行一个整数n,表示数组的大小。
接下来n行,每行n个整数,表示数组的元素。
输出为n*n数组所在行和列的和的最大值。
例如,输入:
```
3
1 2 3
4 5 6
7 8 9
```
输出:
```
24
```
这是因为第3行和第3列的和都是24,是最大值。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)