josiftepe

Untitled

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