2 条题解

  • 0
    @ 2024-5-25 14:56:16
    #include<iostream>
    using namespace std;
    int A[110][110];
    int main(){
    	int n,m;
    	cin >> n >> m;
    	for (int i = 0; i < n; i ++){
    		for (int j = 0; j < m; j ++){
    			cin >> A[i][j]; 
    		}
    	}
    	int a, b, c, d;
    	a = 0, b = 0, c = n - 1, d = m - 1;
    	while(a <= c && b <= d){
    		for (int i = b; i <= d; i ++){
    			cout << A[a][i] << " ";
    		}
    		for (int i = a + 1; i <= c; i ++){
    			cout << A[i][d] << " ";
    		}
    		if(a == c || b == d) break;
    		for (int i = d-1; i >= b; i --){
    			cout << A[c][i] << " ";
    		}
    		for (int i = c - 1; i >= a + 1; i --){
    			cout << A[i][b] << " ";
    		}
    		a++, b++, c--, d--;
    	}
    	return 0;
    } 
    
    • 0
      @ 2023-10-14 9:26:11

      更新一下码风,发一篇草履虫的做法题解

      #include <iostream>
      using namespace std;
      int n, m, arr[100][100], i, l, r, u, d;
      int main() {
          ios::sync_with_stdio(false);
          cin.tie(nullptr), cout.tie(nullptr);
          cin >> n >> m;
          for (i = 0; i < n; ++i)
              for (int j = 0; j < m; ++j) 
                  cin >> arr[i][j];
          l = u = 0, r = m - 1, d = n - 1;
          while (1) {
              for (i = l; i <= r; ++i)
                  cout << arr[u][i] << ' ';
              if (++u > d) break;
              for (i = u; i <= d; ++i)
                  cout << arr[i][r] << ' ';
              if (l > --r) break;
              for (i = r; i >= l; --i)
                  cout << arr[d][i] << ' ';
              if (u > --d) break;
              for (i = d; i >= u; --i)
                  cout << arr[i][l] << ' ';
              if (++l > r) break;
          }
      }
      

      假的,我有更优雅的写法

      #include <iostream>
      using namespace std;
      int n, m, arr[100][100], l, i, j, f, dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
      bool vis[100][100];
      int main() {
          ios::sync_with_stdio(false);
          cin.tie(nullptr), cout.tie(nullptr);
          cin >> n >> m;
          for (i = 0; i < n; ++i)
              for (j = 0; j < m; ++j) 
                  cin >> arr[i][j];
          l = n * m;
          i = j = f = 0;
          for (int _ = 0, ti, tj; _ < l; ++_) {
              cout << arr[i][j] << ' ', vis[i][j] = 1;
              ti = i + dir[f][0], tj = j + dir[f][1];
              if (ti < 0 || ti >= n || tj < 0 || tj >= m || vis[ti][tj])
                  f = (f + 1) % 4;
              i += dir[f][0], j += dir[f][1];
          }
      }
      
      • 1

      信息

      ID
      129
      时间
      1000ms
      内存
      256MiB
      难度
      7
      标签
      (无)
      递交数
      99
      已通过
      26
      上传者