fork download
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. int n, m;
  5. cin >> m >> n;
  6. int s = 1;
  7. int array[m][n];
  8. for (int y = 0; y < n; y++) {
  9. array[0][y] = s;
  10. s++;
  11. }
  12. for (int x = 1; x < m; x++) {
  13. array[x][n - 1] = s;
  14. s++;
  15. }
  16. for (int y = n - 2; y >= 0; y--) {
  17. array[m - 1][y] = s;
  18. s++;
  19. }
  20. for (int x = m - 2; x > 0; x--) {
  21. array[x][0] = s;
  22. s++;
  23. }
  24. int c = 1;
  25. int d = 1;
  26.  
  27. while (s < m * n) {
  28. while (array[c][d + 1] == 0) {
  29. array[c][d] = s;
  30. s++;
  31. d++;
  32. }
  33. while (array[c + 1][d] == 0) {
  34. array[c][d] = s;
  35. s++;
  36. c++;
  37. }
  38. while (array[c][d - 1] == 0) {
  39. array[c][d] = s;
  40. s++;
  41. d--;
  42. }
  43. while (array[c - 1][d] == 0) {
  44. array[c][d] = s;
  45. s++;
  46. c--;
  47. }
  48. }
  49. for (int x = 0; x < m; x++) {
  50. for (int y = 0; y < n; y++) {
  51. if (array[x][y] == 0) {
  52. array[x][y] = s;
  53. }
  54. }
  55. }
  56. for (int x = 0; x < m; x++) {
  57. for (int y = 0; y < n; y++) {
  58. if (array[x][y] < 10) {
  59. cout << array[x][y] << " ";
  60. }
  61. else {
  62. cout << array[x][y] << " ";
  63. }
  64. }
  65. }
  66. return 0;
  67. }
Time limit exceeded #stdin #stdout 5s 15240KB
stdin
4 4
stdout
Standard output is empty