Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1)
- #include <stdio.h>
- #include <stdlib.h>
- #define NULA -1
- typedef unsigned long long Ull;
- typedef struct cancion{
- Ull codigo;
- Ull reproducciones;
- } Cancion;
- long long buscar(Cancion *, Ull , Ull);
- int comparar (const void *, const void *);
- void mostrar (Cancion *, Ull);
- int main()
- {
- Ull cod,rep;
- Ull totalCanciones=0;
- Cancion *canciones = NULL; //vector dinamico
- long long pos;
- do{
- scanf("%llu", &cod);
- if(cod==0)break;
- scanf("%llu",&rep);
- pos = buscar (canciones,totalCanciones,cod);
- if(pos == NULA)
- {
- //insertar nueva cancion
- canciones = (Cancion *) realloc (canciones , sizeof(Cancion) * (totalCanciones+1) );
- if(canciones == NULL) return 0;
- canciones[totalCanciones].codigo = cod;
- canciones[totalCanciones].reproducciones = rep;
- totalCanciones++;
- }
- else
- {
- canciones[pos].reproducciones += rep;
- }
- }while(cod != 0);
- if(totalCanciones != 0)
- {
- qsort(canciones , totalCanciones , sizeof(Cancion) , comparar);
- mostrar(canciones , totalCanciones);
- }
- return 0;
- }
- long long buscar(Cancion *vector, Ull n , Ull clave)
- {
- long long i;
- for(i=0;i<n;i++)
- {
- if(vector[i].codigo == clave)
- {
- return i;
- }
- }
- return NULA;
- }
- int comparar (const void * a, const void * b )
- {
- Cancion *ptrA = (Cancion *) a;
- Cancion *ptrB = (Cancion *) b;
- if(ptrA->reproducciones < ptrB->reproducciones)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- void mostrar (Cancion *vector, Ull n)
- {
- Ull i;
- printf(" _______________________ ________________________\n");
- printf("| CODIGO CANCION | TOTAL REPRODUCCIONES |\n");
- printf("|_______________________|________________________|\n");
- printf("| | |\n");
- for(i=0;i<n;i++)
- {
- printf("| %21llu | %21llu |\n",vector[i].codigo , vector[i].reproducciones);
- }
- printf("|_______________________|________________________|");
- }
- 3)
- #include <stdio.h>
- #include <stdlib.h>
- void mostrar(int *,int);
- int comparar (const void *, const void *);
- int main()
- {
- int p1,p,j,i,flag=0;
- int *perlas = NULL;
- do{
- i=1;
- scanf("%d",&p1);
- if(p1 == 0) return 0;
- do{
- scanf("%d",&p);
- if(p != 0)
- {
- perlas = (int *) realloc (perlas,sizeof(int)*(i+1));
- if(perlas == NULL) return 0;
- perlas[0] = p1;
- perlas[i] = p;
- i++;
- }
- else break;
- }while(p != 0);
- if(i==1)
- {
- printf("%d\n",p1);
- }
- else
- {
- if((i % 2) != 0)
- {
- qsort(perlas,i,sizeof(int),comparar);
- j=0;
- while(j < i-1)
- {
- if(perlas[j] == perlas[j+1])
- {
- flag=1;
- }
- else
- {
- flag=0;
- break;
- }
- j+=2;
- }
- if(flag == 1)
- {
- mostrar(perlas,i);
- }
- else
- {
- printf("NO\n");
- }
- }
- else
- {
- printf("NO\n");
- }
- }
- }while(p1 != 0);
- return 0;
- }
- int comparar(const void * a, const void * b)
- {
- return ( *(int*)a - *(int*)b );
- }
- void mostrar(int *vector,int m)
- {
- int i;
- for(i=0;i<m;i+=2)
- {
- printf("%d ",vector[i]);
- }
- for(i=m-2;i>0;i-=2)
- {
- printf("%d ",vector[i]);
- }
- printf("\n");
- }
- 4)
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct loteria{
- unsigned long long dinero;
- unsigned long long premios;
- }Loteria;
- int comparar (const void *,const void *);
- int comprobar(Loteria *,int);
- int main()
- {
- Loteria *vector = NULL;
- unsigned long long dinero,premios;
- int i=0;
- do{
- scanf("%llu",&dinero);
- if(dinero == 0) break;
- scanf("%llu",&premios);
- vector = (Loteria *) realloc (vector,sizeof(Loteria) * (i+1) );
- vector[i].dinero = dinero;
- vector[i].premios = premios;
- i++;
- }while(dinero != 0);
- if(i != 0)
- {
- qsort(vector,i,sizeof(Loteria),comparar);
- if(comprobar(vector,i))
- {
- printf("SI FUE JUSTA");
- }
- else
- {
- printf("NO FUE JUSTA");
- }
- }
- return 0;
- }
- int comparar (const void * a,const void * b )
- {
- Loteria *ptrA = (Loteria *) a;
- Loteria *ptrB = (Loteria *) b;
- if(ptrA->dinero < ptrB->dinero)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int comprobar(Loteria *vector,int i)
- {
- int j,k;
- for(j=0;j<i-1;j++)
- {
- for(k=j+1;k<i;k++)
- {
- if(vector[j].dinero > vector[k].dinero)
- {
- if(vector[j].premios <= vector[k].premios)
- {
- return 0;
- }
- }
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement