Advertisement
Josif_tepe

Untitled

Jun 7th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int broj;
  7.     scanf("%d", &broj);
  8.     int n; // kolku ke bide golema matricata
  9.    
  10.     for(int i = 1; i <= 20; i++) {
  11.         if(i * i >= broj) {
  12.             n = i;
  13.             break;
  14.         }
  15.     }
  16.     int matrica[n][n];
  17.     for(int i = 0; i < n; i++) {
  18.         for(int j = 0; j < n; j++) {
  19.             matrica[i][j] = 0;
  20.         }
  21.     }
  22.     int brojac = 1;
  23.     for(int j = 0; j < n; j++) {
  24.         if(j % 2 == 0) { // ako imame parna kolona odime od gore nadole
  25.             for(int i = 0; i < n; i++) {
  26.                 if(brojac <= broj) {
  27.                     matrica[i][j] = brojac;
  28.                     brojac++;
  29.                 }
  30.             }
  31.         }
  32.         else {
  33.             for(int i = n - 1; i >= 0; i--) {
  34.                 if(brojac <= broj) {
  35.                     matrica[i][j] = brojac;
  36.                     brojac++;
  37.                 }
  38.             }
  39.         }
  40.     }
  41.    
  42.    
  43.     for(int i = 0; i < n; i++) {
  44.         for(int j = 0; j < n; j++) {
  45.             printf("%d ", matrica[i][j]);
  46.         }
  47.         printf("\n");
  48.     }
  49.     return 0;
  50. }
  51.  
  52.  
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement