注意:
输入是直接输入了一行,所以不能使用for i{ for j {} },可以使用string 读一行然后挨个分析。
使用前缀和。
代码:
#include <bits/stdc++.h> using namespace std; const int N = 105; int n, m; int b[N][N]; string c; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> c; for (int j = 1; j <= m; j++) { b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1] + c[j - 1] - '0'; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cout << b[i][j] << " "; } cout << endl; } return 0; }