|
发表于 2021-11-7 12:49:12
|
显示全部楼层
你的问题表达不太清,我的代码如下,希望对你有帮助:
C 语言:- #include <stdio.h>
- #include <string.h>
- int main()
- {
- // 自行输入所有值,row 表示行,col 表示列
- // int row, col;
- // scanf("%d %d", &row, &col);
- // int arr[row][col];
- // memset(arr, 0, row*col*sizeof(int));
- // for(int i = 0; i < row; i++){
- // for(int j = 0; j < col; j++){
- // scanf("%d", &arr[i][j]);
- // }
- // }
-
- // 范例:假设已经输入全部值,row 为 5,col 为 5
- int row = 5, col = 5;
- int arr[5][5] = {
- {0, 1, 1, 0, 1},
- {1, 0, 1, 1, 0},
- {0, 1, 0, 1, 0},
- {0, 0, 0, 1, 1},
- {1, 1, 0, 0, 0}};
-
- // max 表示最长长方型长度
- int max = 0, res;
-
- // 找出最长长方型长度
- for(int i = 0; i < row; i++){
- res = 0;
- for(int j = 0; j < col; j++){
- if(arr[i][j]){
- res++;
- max = res > max ? res : max;
- }
- else res = 0;
- }
- }
-
- // 行列对调:行变成列,列变成行 row-column exchange
- int temp[row][col];
- for(int i = 0; i < row; i++){
- for(int j = 0; j < col; j++){
- temp[j][i] = arr[i][j];
- }
- }
- for(int i = 0; i < row; i++){
- for(int j = 0; j < col; j++){
- arr[i][j] = temp[i][j];
- }
- }
-
- // 找出最长长方型长度
- for(int i = 0; i < row; i++){
- res = 0;
- for(int j = 0; j < col; j++){
- if(arr[i][j]){
- res++;
- max = res > max ? res : max;
- }
- else res = 0;
- }
- }
-
- printf("%d", max);
- return 0;
- }
复制代码 Python 语言:- # 自行输入所有值,row 表示行,col 表示列
- # row, col = map(int, input().split())
- # arr = []; temp = []
- # for i in range(row):
- # temp = list(map(int, input().split()))
- # arr.append(temp)
- # 范例:假设已经输入全部值
- arr = [
- [0, 1, 1, 0, 1],
- [1, 0, 1, 1, 0],
- [0, 1, 0, 1, 0],
- [0, 0, 0, 1, 1],
- [1, 1, 0, 0, 0]
- ]
- max = 0 # max 表示最长长方型长度
- for row in arr: # 找出最长长方型长度
- res = 0
- for i in row:
- if i:
- res += 1
- max = res if res > max else max
- else:
- res = 0
- arr = [list(col) for col in zip(*arr)] # 行列对调:行变成列,列变成行 row-column exchange
- for row in arr: # 找出最长长方型长度
- res = 0
- for i in row:
- if i:
- res += 1
- max = res if res > max else max
- else:
- res = 0
- print(max)
复制代码 输出结果: |
|