Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct interval
- {
- int low, high;
- };
- void citire(FILE *fin, int *n, int *a, int *b, struct interval *ptr)
- {
- fscanf(fin,"%d",&(*a));
- fscanf(fin,"%d",&(*b));
- fscanf(fin,"%d",&(*n));
- int i;
- for (i=0; i<*n; i++)
- {
- fscanf(fin,"%d %d",&ptr->low,&ptr->high);
- ptr++;
- }
- }
- void afisare(FILE *fout, int n, int a, int b, struct interval *ptr)
- {
- fprintf(fout,"Vector sortat crescator dupa high:\n");
- int i;
- fprintf(fout,"Intervalul [%d,%d]\n",a,b);
- fprintf(fout,"%d\n",n);
- for (i=0; i<n; i++)
- {
- fprintf(fout,"%d %d \n", ptr->low, ptr->high);
- ptr++;
- }
- }
- int comparator(const void *s1, const void *s2) //asta e pentru quicksort
- {
- struct interval *e1 = (struct interval *) s1;
- struct interval *e2 = (struct interval *) s2;
- return e1->high - e2->high;
- }
- void greedy(FILE *fout, int n, int a, int b, struct interval *v)
- {
- fprintf(fout,"\n********************************************************\nRezolvare:\n");
- int i=0, low_aux, high_aux;
- while (a <= b)
- {
- while (v[i].low <= a && v[i].high > a && i<n)
- {
- low_aux = v[i].low;
- high_aux = v[i].high;
- i++;
- }
- a = high_aux;
- fprintf(fout,"%d %d\n",low_aux,high_aux);
- }
- }
- int main()
- {
- FILE *fin, *fout;
- fin = fopen("date.in","r");
- fout = fopen("date.out","w");
- struct interval vec[100];
- int n,a,b;
- citire(fin,&n,&a,&b,vec);
- qsort(vec,n,sizeof(struct interval),comparator);
- afisare(fout,n,a,b,vec);
- greedy(fout,n,a,b,vec);
- return 0;
- }
- //cod luat
- #include <stdio.h>
- #include <stdlib.h>
- struct Interval
- {
- int a,b;
- };
- int cmp (const void *p, const void *q)
- {
- struct Interval vp = *(struct Interval *)p;
- struct Interval vq = *(struct Interval *)q;
- return vp.b - vq.b;
- }
- int main()
- {
- int n , i, A, B, aaux, baux;
- printf("n="); scanf("%d", &n);
- printf("A="); scanf("%d", &A);
- printf("B="); scanf("%d", &B);
- printf("Intervalele: ");
- struct Interval v[100];
- for(i = 0; i < n; i++)
- scanf("%d%d", &v[i].a, &v[i].b);
- qsort (v, n, sizeof(struct Interval), cmp);
- i=0;
- while (A <=B)
- {
- while (v[i].a <= A && v[i].b > A && i<n)
- {
- aaux = v[i].a;
- baux = v[i].b;
- i++;
- }
- A=baux;
- printf("[%d,%d]\n", aaux, baux);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment