Advertisement
icatalin

lab 5 tp 23.03.2018

Mar 23rd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. //prob 1
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. int main()
  8. {
  9.     FILE *fout,*fin;
  10.     fout=fopen("date.out","w");
  11.     fin=fopen("date.in","r");
  12.  
  13.     int x,i,a,b,n; // ordinea in fisier: n, a, b
  14.  
  15.     fscanf(fin,"%d %d %d",&n,&a,&b);
  16.  
  17.     srand(time(NULL));
  18.  
  19.     for (i=0; i<n; i++)
  20.     {
  21.         x = rand()%(b-a+1)+a;
  22.         fprintf(fout,"%d ",x);
  23.     }
  24.  
  25.     return 0;
  26. }
  27.  
  28. //prob2
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <time.h>
  32. /*
  33. int main()
  34. {
  35.     FILE *fout,*fin;
  36.     fout=fopen("date.out","w");
  37.     fin=fopen("date.in","r");
  38.  
  39.     int x,i,a,b,n; // ordinea in fisier: n, a, b
  40.  
  41.     fscanf(fin,"%d %d %d",&n,&a,&b);
  42.  
  43.     srand(time(NULL));
  44.  
  45.     for (i=0; i<n; i++)
  46.     {
  47.         x = rand()%(b-a+1)+a;
  48.         fprintf(fout,"%d ",x);
  49.     }
  50.  
  51.  
  52.  
  53.  
  54.     return 0;
  55. }
  56. */
  57.  
  58. int main()
  59. {
  60.     FILE *fin;
  61.     fin=fopen("date.in","r");
  62.  
  63.     int x,v[100],i,j,ok = 0;
  64.  
  65.     fscanf(fin,"%d",&x);
  66.     printf("x = %d\n",x);
  67.  
  68.     for (i=0; i<10; i++)
  69.         fscanf(fin,"%d",&v[i]);
  70.     for (i=0; i<10; i++)
  71.         printf("v[%d] = %d \n",i,v[i]);
  72.  
  73.     clock_t a,b;
  74.     a = clock();
  75.  
  76.     for (i=0; i<10; i++)
  77.         {for (j=0; j<10; j++)
  78.             if ( v[i] + v[j] == x  )
  79.                 {printf("\nNumarul %d si numarul %d, de pe poz. %d, repsectiv %d adunate dau numarul %d.\n",v[i],v[j],i,j,x);
  80.                     break;
  81.                 }
  82.             if (j<10)
  83.                 break;
  84.         }
  85.  
  86.  
  87.  
  88.     b = clock();
  89.     float t = (float)(b-a)/CLOCKS_PER_SEC;
  90.     printf("\nTimpul necesar a fost de %f\n\n",(b-a)/CLOCKS_PER_SEC);
  91.  
  92.     return 0;
  93. }
  94.  
  95. //problema 2 varianta 2
  96. int main()
  97. {
  98.     FILE *fin;
  99.     fin=fopen("date.in","r");
  100.  
  101.     int x,v[100],i,j,ok = 0;
  102.  
  103.     fscanf(fin,"%d",&x);
  104.     printf("x = %d\n",x);
  105.  
  106.     for (i=0; i<10; i++)
  107.         fscanf(fin,"%d",&v[i]);
  108.     for (i=0; i<10; i++)
  109.         printf("v[%d] = %d \n",i,v[i]);
  110.  
  111.     clock_t a,b;
  112.     a = clock();
  113.  
  114.     i = 0; j = 9;
  115.     int s;
  116.  
  117.     while (i <=9 && j>=0 )
  118.     {
  119.         s = 0;
  120.         s = v[i] + v[j];
  121.  
  122.         if (s == x && i!=j)
  123.         {
  124.             printf("\nNumarul %d si numarul %d, de pe poz. %d, repsectiv %d adunate dau numarul %d.\n",v[i],v[j],i,j,x);
  125.                     break;
  126.         }
  127.         else if (s < x)
  128.             i++;
  129.         else if (s > x)
  130.             j--;
  131.     }
  132.  
  133.     b = clock();
  134.     float t = (float)(b-a)/CLOCKS_PER_SEC;
  135.     printf("\nTimpul necesar a fost de %.20f\n\n",t);
  136.  
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement