Advertisement
Lagx

Cuestionario #1 Módulo 4 - vectores dinámicos

Oct 27th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.61 KB | None | 0 0
  1. 1)
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define NULA -1
  5.  
  6. typedef unsigned long long Ull;
  7.  
  8. typedef struct cancion{
  9.     Ull codigo;
  10.     Ull reproducciones;
  11. } Cancion;
  12.  
  13. long long buscar(Cancion *, Ull , Ull);
  14. int comparar (const void *, const void *);
  15. void mostrar (Cancion *, Ull);
  16.  
  17. int main()
  18. {
  19.     Ull cod,rep;
  20.     Ull totalCanciones=0;
  21.     Cancion *canciones = NULL; //vector dinamico
  22.     long long pos;
  23.    
  24.     do{
  25.         scanf("%llu", &cod);
  26.         if(cod==0)break;
  27.         scanf("%llu",&rep);
  28.         pos = buscar (canciones,totalCanciones,cod);
  29.         if(pos == NULA)
  30.         {
  31.             //insertar  nueva  cancion
  32.             canciones = (Cancion *) realloc (canciones , sizeof(Cancion) * (totalCanciones+1) );
  33.             if(canciones == NULL) return 0;
  34.             canciones[totalCanciones].codigo = cod;
  35.             canciones[totalCanciones].reproducciones = rep;
  36.             totalCanciones++;    
  37.         }
  38.         else
  39.         {
  40.             canciones[pos].reproducciones += rep;
  41.         }
  42.     }while(cod != 0);
  43.    
  44.     if(totalCanciones != 0)
  45.     {
  46.         qsort(canciones , totalCanciones , sizeof(Cancion) , comparar);
  47.         mostrar(canciones , totalCanciones);
  48.     }
  49.    
  50.     return 0;
  51. }
  52.  
  53. long long buscar(Cancion *vector, Ull n , Ull clave)
  54. {
  55.     long long i;
  56.     for(i=0;i<n;i++)
  57.     {
  58.         if(vector[i].codigo == clave)
  59.         {
  60.             return i;
  61.         }
  62.     }
  63.     return NULA;
  64. }
  65.  
  66. int comparar (const void * a, const void * b )
  67. {
  68.     Cancion *ptrA = (Cancion *) a;
  69.     Cancion *ptrB = (Cancion *) b;
  70.     if(ptrA->reproducciones < ptrB->reproducciones)
  71.     {
  72.         return 1;
  73.     }
  74.     else
  75.     {
  76.         return 0;
  77.     }
  78. }
  79.  
  80. void mostrar (Cancion *vector, Ull n)
  81. {
  82.     Ull i;
  83.    
  84.     printf(" _______________________ ________________________\n");
  85.     printf("|    CODIGO CANCION     |  TOTAL REPRODUCCIONES  |\n");
  86.     printf("|_______________________|________________________|\n");
  87.     printf("|                       |                        |\n");      
  88.  
  89.     for(i=0;i<n;i++)
  90.     {
  91.         printf("| %21llu | %21llu  |\n",vector[i].codigo , vector[i].reproducciones);
  92.     }
  93.    
  94.     printf("|_______________________|________________________|");    
  95. }
  96.  
  97. 3)
  98. #include <stdio.h>
  99. #include <stdlib.h>
  100.  
  101. void mostrar(int *,int);
  102. int comparar (const void *, const void *);
  103.  
  104. int main()
  105. {
  106.     int p1,p,j,i,flag=0;
  107.     int *perlas = NULL;
  108.    
  109.     do{
  110.         i=1;
  111.         scanf("%d",&p1);
  112.         if(p1 == 0) return 0;
  113.        
  114.         do{
  115.             scanf("%d",&p);
  116.             if(p != 0)
  117.             {
  118.                 perlas = (int *) realloc (perlas,sizeof(int)*(i+1));
  119.                 if(perlas == NULL) return 0;
  120.                 perlas[0] = p1;
  121.                 perlas[i] = p;
  122.                 i++;
  123.             }
  124.             else break;
  125.         }while(p != 0);
  126.        
  127.         if(i==1)
  128.         {
  129.             printf("%d\n",p1);
  130.         }
  131.         else
  132.         {
  133.             if((i % 2) != 0)
  134.             {
  135.                 qsort(perlas,i,sizeof(int),comparar);
  136.                
  137.                 j=0;
  138.                 while(j < i-1)
  139.                 {
  140.                     if(perlas[j] == perlas[j+1])
  141.                     {
  142.                         flag=1;
  143.                     }
  144.                     else
  145.                     {
  146.                         flag=0;
  147.                         break;
  148.                     }
  149.                     j+=2;
  150.                 }
  151.                
  152.                 if(flag == 1)
  153.                 {
  154.                     mostrar(perlas,i);
  155.                 }
  156.                 else
  157.                 {
  158.                     printf("NO\n");
  159.                 }
  160.             }
  161.             else
  162.             {
  163.                 printf("NO\n");
  164.             }
  165.         }    
  166.     }while(p1 != 0);
  167.    
  168.     return 0;
  169. }
  170.  
  171. int comparar(const void * a, const void * b)
  172. {
  173.     return ( *(int*)a - *(int*)b );
  174. }
  175.  
  176. void mostrar(int *vector,int m)
  177. {
  178.     int i;
  179.     for(i=0;i<m;i+=2)
  180.     {
  181.         printf("%d ",vector[i]);
  182.     }
  183.     for(i=m-2;i>0;i-=2)
  184.     {
  185.         printf("%d ",vector[i]);
  186.     }
  187.     printf("\n");
  188. }
  189.  
  190. 4)
  191. #include <stdio.h>
  192. #include <stdlib.h>
  193.  
  194. typedef struct loteria{
  195.     unsigned long long dinero;
  196.     unsigned long long premios;
  197. }Loteria;
  198.  
  199. int comparar (const void *,const void *);
  200. int comprobar(Loteria *,int);
  201.  
  202. int main()
  203. {
  204.     Loteria *vector = NULL;
  205.     unsigned long long dinero,premios;
  206.     int i=0;
  207.     do{
  208.         scanf("%llu",&dinero);
  209.         if(dinero == 0) break;
  210.         scanf("%llu",&premios);
  211.         vector = (Loteria *) realloc (vector,sizeof(Loteria) * (i+1) );
  212.         vector[i].dinero = dinero;
  213.         vector[i].premios = premios;
  214.         i++;
  215.     }while(dinero != 0);
  216.    
  217.     if(i != 0)
  218.     {
  219.         qsort(vector,i,sizeof(Loteria),comparar);
  220.        
  221.         if(comprobar(vector,i))
  222.         {
  223.             printf("SI FUE JUSTA");
  224.         }
  225.         else
  226.         {
  227.             printf("NO FUE JUSTA");
  228.         }
  229.     }
  230.    
  231.     return 0;
  232. }
  233.  
  234. int comparar (const void * a,const void * b )
  235. {
  236.     Loteria *ptrA = (Loteria *) a;
  237.     Loteria *ptrB = (Loteria *) b;
  238.     if(ptrA->dinero < ptrB->dinero)
  239.     {
  240.         return 1;
  241.     }
  242.     else
  243.     {
  244.         return 0;
  245.     }
  246. }
  247.  
  248. int comprobar(Loteria *vector,int i)
  249. {
  250.     int j,k;
  251.    
  252.     for(j=0;j<i-1;j++)
  253.     {
  254.         for(k=j+1;k<i;k++)
  255.         {
  256.             if(vector[j].dinero > vector[k].dinero)
  257.             {
  258.                 if(vector[j].premios <= vector[k].premios)
  259.                 {
  260.                     return 0;
  261.                 }
  262.             }
  263.         }
  264.     }
  265.    
  266.     return 1;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement