NC221778. MatrixProblem
描述
输入描述
The first line contains two integers and -- the size of the matrices.
The next lines describe the matrix . Each of the following lines contains characters or .
It is guaranteed that the boundaries of the matrix are , i.e., the -st row, the -st column, the -th row and the -th column of the matrix are .
输出描述
Print lines and each line has characters or .
The first lines describe the matrix .
The next lines describe the matrix .
If there are multiple answers, print any of them.
示例1
输入:
5 5 00000 00100 01010 01100 00000
输出:
00000 00100 01110 01100 00000 00000 01110 01010 01110 00000
C++(g++ 7.5.0) 解法, 执行用时: 55ms, 内存消耗: 1376K, 提交时间: 2023-03-25 22:52:14
#include<bits/stdc++.h> using namespace std; int n, m; char g[505][505]; int main(){ cin>>n>>m; for(int i = 1; i<=n; ++i){ for(int j = 1; j<=m; ++j){ cin>>g[i][j]; } } for(int i = 1; i<=n; ++i){ for(int j = 1; j<=m; ++j){ if((i%2&&j!=m) || j==1 || g[i][j]=='1') cout<<1; else cout<<0; } cout<<"\n"; } for(int i = 1; i<=n; ++i){ for(int j = 1; j<=m; ++j){ if((i%2==0&&j!=1) || j==m || g[i][j]=='1') cout<<1; else cout<<0; } cout<<"\n"; } return 0; } /* */
C++(clang++11) 解法, 执行用时: 12ms, 内存消耗: 1656K, 提交时间: 2021-05-15 16:28:58
#include<bits/stdc++.h> using namespace std; string a[501],b[501]; int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ cin>>a[i]; b[i]=a[i]; } for(int i=1;i<n-1;i+=2) for(int j=1;j<m-1;j++) a[i][j]='1'; for(int i=2;i<n-1;i+=2) for(int j=1;j<m-1;j++) b[i][j]='1'; for(int i=1;i<n-1;i++) b[i][m-1]=a[i][0]='1'; for(int i=0;i<n;i++)cout<<a[i]<<endl; for(int i=0;i<n;i++)cout<<b[i]<<endl; }
pypy3(pypy3.6.1) 解法, 执行用时: 68ms, 内存消耗: 20764K, 提交时间: 2021-05-13 23:43:44
n, m = map(int, input().split()) res = [] for _ in range(n) : res.append(input()) for i in range(n) : if i & 1 : print('1' + res[i][1:]) else : print('1' * (m - 1) + '0') for i in range(n) : if i & 1 : print('0' + '1' * (m - 1)) else : print(res[i][:-1] + '1')