fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. #define max 1000
  7.  
  8. int mat[max][max];
  9.  
  10. int main() {
  11. // int mat[max][max];
  12. int n;
  13. scanf("%d", &n);
  14.  
  15. int num = 1;
  16. for (int d = 0; d <= 2 * (n - 1); d++) {
  17. if (d % 2 == 0) {
  18. for (int i = n - 1; i >= 0; i--) {
  19. int j = d - i;
  20. if (j >= 0 && j < n) {
  21. mat[i][j] = num++;
  22. }
  23. }
  24. } else {
  25. for (int i = 0; i < n; i++) {
  26. int j = d - i;
  27. if (j >= 0 && j < n) {
  28. mat[i][j] = num++;
  29. }
  30. }
  31. }
  32. }
  33.  
  34. for (int i = 0; i < n; i++) {
  35. for (int j = 0; j < n; j++) {
  36. printf("%d", mat[i][j]);
  37. if (j != n - 1) printf(" ");
  38. }
  39. printf("\n");
  40. }
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5288KB
stdin
4
stdout
1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16