fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int TEN = 10;
  5.  
  6. bool notPerfectSquare(int x) {
  7. int div = 0;
  8. while (div * div < x) {
  9. ++div;
  10. }
  11. return div * div != x;
  12. }
  13.  
  14. int nextPerfectSquare(int& x) {
  15. do {
  16. ++x;
  17. } while (notPerfectSquare(x));
  18. return x;
  19. }
  20.  
  21. void generateMatrix(int matrix[][TEN], int n, int x) {
  22. int firstRow = 0, firstColumn = 0;
  23. int lastRow = n - 1, lastColumn = n - 1;
  24. while (firstRow <= lastRow && firstColumn <= lastColumn) {
  25. for (int i = lastRow; i >= firstRow; --i) {
  26. matrix[i][firstColumn] = nextPerfectSquare(x);
  27. }
  28. for (int j = firstColumn + 1; j <= lastColumn; ++j) {
  29. matrix[firstRow][j] = nextPerfectSquare(x);
  30. }
  31. for (int i = firstRow + 1; i <= lastRow && firstColumn < lastColumn; ++i) {
  32. matrix[i][lastColumn] = nextPerfectSquare(x);
  33. }
  34. for (int j = lastColumn - 1; j > firstColumn && firstRow < lastRow; --j) {
  35. matrix[lastRow][j] = nextPerfectSquare(x);
  36. }
  37. ++firstRow;
  38. ++firstColumn;
  39. --lastRow;
  40. --lastColumn;
  41. }
  42. }
  43.  
  44. int main() {
  45. int n, x, matrix[TEN][TEN];
  46. cin >> n >> x;
  47. generateMatrix(matrix, n, x);
  48. for (int i = 0; i < n; ++i) {
  49. for (int j = 0; j < n; ++j) {
  50. cout << matrix[i][j] << ' ';
  51. }
  52. cout << '\n';
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5272KB
stdin
10 99
stdout
361 400 441 484 529 576 625 676 729 784 
324 2809 2916 3025 3136 3249 3364 3481 3600 841 
289 2704 6241 6400 6561 6724 6889 7056 3721 900 
256 2601 6084 9409 9604 9801 10000 7225 3844 961 
225 2500 5929 9216 11449 11664 10201 7396 3969 1024 
196 2401 5776 9025 11236 11881 10404 7569 4096 1089 
169 2304 5625 8836 11025 10816 10609 7744 4225 1156 
144 2209 5476 8649 8464 8281 8100 7921 4356 1225 
121 2116 5329 5184 5041 4900 4761 4624 4489 1296 
100 2025 1936 1849 1764 1681 1600 1521 1444 1369