Advertisement
Guest User

2.2.c

a guest
Sep 30th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int N,p,**post,i=-1,l,j=-1,pocitadlo=1,k,pomocne=0,o=-1,a=-2147483648,b=2147483647;
  7.     char c;
  8.  
  9.     while((scanf("%d",&N)) > 0 )
  10.     {
  11.       if((N <= 1) || (N >= 30000))
  12.         break;
  13.     post = (int **) malloc((N+1) * sizeof(int *));
  14.     for(k = 0; k <=N; k++)
  15.         post[k] = (int *) malloc((N+1) * sizeof(int));
  16.     for(k = 0; k<= N; k++)
  17.         post[k][0]=0;
  18.     while((scanf("%d",&p) > 0))
  19.     {
  20.       if((p < a) || (p > b))
  21.         return 0;
  22.        
  23.         if(o == -1)
  24.         {
  25.             post[0][0]=p;
  26.             post[0][1]=0;
  27.             o++;
  28.         }
  29.         else
  30.         {
  31.             for(i = 0; i <= N; i++)
  32.             {
  33.                 if((post[i][0] == 0) && (post[i+1][0] == 0))
  34.                     break;
  35.            
  36.                 j=0;
  37.                 while(post[i][j+1] != 0)
  38.                     j++;
  39.                 if(p > post[i][j])
  40.                 {
  41.                     post[i][j+2]=post[i][j+1];
  42.                     post[i][++j]=p;
  43.                     break;
  44.                
  45.                 }
  46.                 else
  47.                 {
  48.                 if(p < post[i][0])
  49.                 {
  50.                     for(k = j; k >=-1; k--)
  51.                     post[i][k+2]=post[i][k+1];
  52.                     post[i][0]=p;
  53.                     j++;
  54.                     break;
  55.                 }
  56.                 else
  57.                 {
  58.                     if((post[i+1][0] == 0) && (p <= post[i][j]) && (p >= post[i][0]))
  59.                     {
  60.                         post[i+1][1]=0;
  61.                         post[i+1][0]=p;
  62.                         pocitadlo++;
  63.                         break;
  64.                     }
  65.                    
  66.                 }
  67.                 }
  68.                        
  69.             }
  70.         }
  71.  
  72.         c=getchar();
  73.         if(c == '\n')
  74.             break;
  75.     }
  76.     printf("%d\n",pocitadlo);
  77.     for(k = 0; k <=N; k++)
  78.     {
  79.         if(post[k][0] == 0)
  80.             break;
  81.         j=0;
  82.         while((post[k][j] != 0))
  83.         {
  84.             printf("%d ",post[k][j]);
  85.             j++;
  86.         }
  87.  //     if((post[k][j] == 0) && (post[k+1][0] != 0))
  88.         printf("\n");
  89.  
  90.     }
  91.     for(k=0; k <= N; k++)
  92.         free(post[k]);
  93.     free(post);
  94.     post = NULL;
  95.     pocitadlo=1;
  96.     o=-1;
  97.     }
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement