Alx09

Planificare

May 18th, 2020
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct{
  5.     int x, y, k;
  6. }activitate;
  7.  
  8. activitate a[20];
  9. int n, s[20], m;
  10.  
  11. void citeste()
  12. {
  13.     int i;
  14.     FILE *f;
  15.     f = fopen("in.txt", "r");
  16.     fscanf(f, "%d", &n);
  17.     for (i = 1; i <= n; i++) {
  18.         fscanf(f, "%d%d", &a[i].x, &a[i].y);
  19.         a[i].k = i;
  20.     }
  21.     fclose(f);
  22. }
  23. void sort()
  24. {
  25.     int i, j; activitate aux;
  26.     for (i = 1; i < n; i++)
  27.         for (j = i; j <= n; j++)
  28.             if (a[i].y >= a[j].y) {
  29.                 aux = a[i];
  30.                 a[i] = a[j];
  31.                 a[j] = aux;
  32.             }
  33. }
  34. void greedy()
  35. {
  36.     int i, j; s[1] = 1; j = 1;
  37.     for (i = 2; i <= n; i++)
  38.         if (a[i].x >= a[s[j]].y) {
  39.             j++;
  40.             s[j] = i; }
  41.     m = j;
  42. }
  43.  
  44. void afiseaza()
  45. {
  46.     printf( "Planificarea activitatilor: \n");
  47.     for (int i = 1; i <= m; i++)
  48.         printf( "Activitatea %d incepe la ora  %d si se termina la ora %d.\n", a[s[i]].k, a[s[i]].x, a[s[i]].y);
  49.    
  50. }
  51. int main()
  52. {
  53.     citeste();
  54.     sort();
  55.     greedy();
  56.     afiseaza();
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment