fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int TEN = 10;
  5.  
  6. void generateMatrix(int matrix[][TEN], int n, int x) {
  7. int firstColumn = 0, lastColumn = n - 1;
  8. int firstRow = 0, lastRow = n - 1;
  9. while (firstRow < lastRow) {
  10. for (int i = firstColumn; i <= lastColumn; ++i) {
  11. ++x;
  12. matrix[firstColumn][i] = x * x;
  13. }
  14. for (int i = firstRow + 1; i <= lastRow; ++i) {
  15. ++x;
  16. matrix[lastColumn][i] = x * x;
  17. }
  18. for (int i = lastColumn - 1; i >= firstColumn && firstRow != lastRow; --i) {
  19. ++x;
  20. matrix[lastRow][i] = x * x;
  21. }
  22. for (int i = lastRow - 1; i > firstRow && firstColumn != lastColumn; --i) {
  23. ++x;
  24. matrix[lastRow][i] = x * x;
  25. }
  26. ++firstRow;
  27. ++firstColumn;
  28. --lastRow;
  29. --lastColumn;
  30. }
  31. }
  32.  
  33. int main() {
  34. int n, x, matrix[TEN][TEN];
  35. cin >> n >> x;
  36. generateMatrix(matrix, n, x);
  37. for (int i = 0; i < n; ++i) {
  38. for (int j = 0; j < n; ++j) {
  39. cout << matrix[i][j] << ' ';
  40. }
  41. cout << '\n';
  42. }
  43. return 0;
  44. }
Success #stdin #stdout 0s 5276KB
stdin
4
stdout
1 4 9 16 
0 169 196 0 
0 256 225 5390 
100 144 121 49