Advertisement
icatalin

Tema lab 6

Apr 11th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.98 KB | None | 0 0
  1. //problema 1 - interclasare
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main()
  7. {
  8.     // punctul a
  9.     FILE *fin,*fout;
  10.  
  11.     fin = fopen("date.in","r");
  12.     fout = fopen("date.out","w");
  13.  
  14.     int a[100],b[100],c[100];
  15.     int i, j, n, m, p = 0;
  16.  
  17.     fscanf(fin,"%d",&n);
  18.     for (i=0; i<n; i++)
  19.         fscanf(fin,"%d",&a[i]);
  20.  
  21.     fscanf(fin,"%d",&m);
  22.     for (i=0; i<m; i++)
  23.         fscanf(fin,"%d",&b[i]);
  24.  
  25.     i=0; j=0;
  26.  
  27.     while (i < n && j < m)
  28.         if (a[i] < b[j])
  29.         {
  30.             c[p] = a[i];
  31.             p++;
  32.             i++;
  33.         }
  34.         else
  35.         {
  36.             c[p] = b[j];
  37.             p++;
  38.             j++;
  39.         }
  40.  
  41.     if (i <= n)
  42.     {
  43.         int k;
  44.  
  45.         for (k = i; k < n; k++)
  46.         {
  47.             c[p] = a[k];
  48.             p++;
  49.         }
  50.     }
  51.  
  52.     if (j <= m)
  53.     {
  54.         int k;
  55.  
  56.         for (k = j; k < m; k++)
  57.         {
  58.             c[p] = b[k];
  59.             p++;
  60.         }
  61.     }
  62.  
  63.     for (i = 0; i < p; i++)
  64.         fprintf(fout,"%d ",c[i]);
  65.  
  66.     // punctul b
  67.     fprintf(fout,"\n");
  68.  
  69.     i = p - 1;
  70.     int nr_crt = -1;
  71.     while (i >= 0)
  72.     {
  73.         if ((c[i] == c[i-1]) && (c[i] != nr_crt))
  74.         {
  75.             fprintf(fout,"%d ",c[i]);
  76.             nr_crt = c[i];
  77.         }
  78.         i--;
  79.     }
  80.  
  81.     return 0;
  82. }
  83.  
  84. //problema 1 punctul c
  85. #include <stdio.h>
  86. #include <stdlib.h>
  87.  
  88. int main()
  89. {
  90.     // punctul c
  91.     FILE *fin,*fout;
  92.  
  93.     fin = fopen("date.in","r");
  94.     fout = fopen("date.out","w");
  95.  
  96.     int a[100],b[100],c[100];
  97.     int i, j, n, m, p = 0;
  98.  
  99.     fscanf(fin,"%d",&n);
  100.     for (i=0; i<n; i++)
  101.         fscanf(fin,"%d",&a[i]);
  102.  
  103.     fscanf(fin,"%d",&m);
  104.     for (i=0; i<m; i++)
  105.         fscanf(fin,"%d",&b[i]);
  106.  
  107.     i=0; j= m - 1;
  108.  
  109.     while (i < n && j >= 0)
  110.         if (a[i] < b[j])
  111.         {
  112.             if (a[i] % 2 == 0)
  113.             {
  114.  
  115.                 c[p] = a[i];
  116.                 p++;
  117.             }
  118.             i++;
  119.         }
  120.         else
  121.         {
  122.             if (b[j] % 2 == 0)
  123.             {
  124.                 c[p] = b[j];
  125.                 p++;
  126.             }
  127.             j--;
  128.         }
  129.  
  130.     if (i <= n)
  131.     {
  132.         int k;
  133.  
  134.         for (k = i; k < n; k++)
  135.             if (a[k] % 2 == 0)
  136.             {
  137.                 c[p] = a[k];
  138.                 p++;
  139.             }
  140.  
  141.     }
  142.  
  143.     if (j >= 0)
  144.     {
  145.         int k;
  146.  
  147.         for (k = j; k >= 0; k--)
  148.             if (b[k] % 2 == 0)
  149.             {
  150.                 c[p] = b[k];
  151.                 p++;
  152.             }
  153.  
  154.     }
  155.  
  156.     for (i = 0; i < p; i++)
  157.         fprintf(fout,"%d ",c[i]);
  158.  
  159.     return 0;
  160. }
  161.  
  162. // problema 2 - paranteze
  163.  
  164. #include <stdio.h>
  165. #include <stdlib.h>
  166. #include <string.h>
  167.  
  168. int main()
  169. {
  170.  
  171.     FILE *fin,*fout;
  172.  
  173.     fin = fopen("date.in","r");
  174.     fout = fopen("date.out","w");
  175.  
  176.     char s[30];
  177.     int i, c = 0, total = 0;
  178.  
  179.     fscanf(fin,"%s",s);
  180.  
  181.  
  182.     for (i = 0; i < strlen(s); i++)
  183.     {
  184.         if (s[i] == '(')
  185.             { c++; total++; }
  186.         else if (s[i] == ')')
  187.             { c--; total++; }
  188.  
  189.         if (c < 0)
  190.         {
  191.             fprintf(fout,"Secventa de paranteze este gresita.");
  192.             break;
  193.         }
  194.     }
  195.  
  196.    if (c == 0 && total)
  197.         fprintf(fout,"Secventa de paranteze este corecta.");
  198.     else if (c > 0)
  199.         fprintf(fout,"Secventa de paranteze este gresita.");
  200.  
  201.  
  202.     return 0;
  203. }
  204.  
  205. // problema 2 - punctul b
  206. #include <stdio.h>
  207. #include <stdlib.h>
  208. #include <string.h>
  209.  
  210. int main()
  211. {
  212.  
  213.     FILE *fin,*fout;
  214.  
  215.     fin = fopen("date.in","r");
  216.     fout = fopen("date.out","w");
  217.  
  218.     char s[30];
  219.     int i, c = 0, cd = 0, total = 0, totald = 0, gasit_dr = 0, gasit_ro = 0, ordine_ok = 1;
  220.  
  221.     fscanf(fin,"%s",s);
  222.  
  223.  
  224.     for (i = 0; i < strlen(s); i++)
  225.     {
  226.         if (s[i] == '[')
  227.             { cd++; totald++; gasit_dr = 1; }
  228.         else if (s[i] == ']')
  229.             { cd--; totald++; gasit_dr = 1; }
  230.  
  231.         if (s[i] == '(')
  232.             { c++; total++; gasit_ro = 1; }
  233.         else if (s[i] == ')')
  234.             { c--; total++; gasit_ro = 1; }
  235.  
  236.         if (gasit_ro == 1 && gasit_dr == 0)
  237.         {
  238.             ordine_ok = 0;
  239.             fprintf(fout,"Secventa de paranteze este gresita.");
  240.             return 0;
  241.         }
  242.  
  243.         if (c < 0)
  244.         {
  245.             fprintf(fout,"Secventa de paranteze este gresita.");
  246.             return 0;
  247.         }
  248.  
  249.         if (cd < 0)
  250.         {
  251.             fprintf(fout,"Secventa de paranteze este gresita.");
  252.             return 0;
  253.         }
  254.  
  255.     }
  256.  
  257.     if (ordine_ok == 0)
  258.         { fprintf(fout,"Secventa de paranteze este gresita."); return 0; }
  259.     else if (c > 0)
  260.         { fprintf(fout,"Secventa de paranteze este gresita."); return 0; }
  261.     else if (cd > 0)
  262.         { fprintf(fout,"Secventa de paranteze este gresita."); return 0; }
  263.     else if (c == 0 && total && cd == 0 && totald)
  264.         fprintf(fout,"Secventa de paranteze este corecta.");
  265.  
  266.  
  267.     return 0;
  268. }
  269.  
  270. // problema 3
  271.  
  272. #include <stdio.h>
  273. #include <stdlib.h>
  274.  
  275. FILE *fin,*fout;
  276.  
  277. int v[100], n, ok;
  278. int varfuri[100], k;
  279.  
  280. void citire()
  281. {
  282.     fin = fopen("date.in","r");
  283.     int i = 0;
  284.  
  285.     while ( fscanf(fin,"%d",&v[i++]) == 1)
  286.     n = i;
  287.  
  288.     printf("%d\n",n);
  289.  
  290.     for (i=0; i<n; i++)
  291.         printf("%d ",v[i]);
  292.  
  293.     fclose(fin);
  294.  
  295. }
  296.  
  297. void secventeMunte()
  298. {
  299.     int i, varf, pozVarf, cresc, descresc;
  300.  
  301.     ok = 0; //fals
  302.  
  303.     i = 0;
  304.  
  305.     while (i < n - 1)
  306.     {
  307.         cresc = descresc = 0;
  308.  
  309.         while (v[i] < v[i+1] && i < n - 1) // "urcam muntele" pana ajungem la varf
  310.         { i++; cresc++; } // cresc numara secventa munte
  311.  
  312.         varf = v[i]; pozVarf = i; // memoram valoare si pozitia varfului
  313.  
  314.         while (v[i] > v[i+1] && i < n -1)
  315.         { i++; descresc++; } // descr numara secventa munte
  316.  
  317.         if (cresc + descresc >= 2 && cresc > 0 && descresc > 0) // stabilim daca am gasit o secventa munte
  318.         {
  319.             varfuri[k++] = varf; // salvam varfurile intr-un alt vector
  320.             v[pozVarf] = -1; // marcam varfurile in vectorul initial
  321.             ok = 1; // am gasit cel putin o secventa munte
  322.         }
  323.  
  324.     }
  325.  
  326. }
  327.  
  328. void eliminareVarfuri()
  329. {
  330.     int i, k;
  331.     i = 0;
  332.  
  333.     while (i < n)
  334.     {
  335.         if (v[i] == -1) // daca gasim un varf il eliminam
  336.         {
  337.             for (k = i; k < n - 1; k++)
  338.                 v[k] = v[k + 1];
  339.             n--;
  340.         }
  341.         else
  342.             i++;
  343.     }
  344. }
  345.  
  346. int main()
  347. {
  348.  
  349.     fout = fopen("date.out","w");
  350.     citire();
  351.  
  352.     secventeMunte();
  353.     eliminareVarfuri();
  354.     fprintf(fout,"%d\n",k); // punctul a, afisam numarul de secvente munte din vectorul initial
  355.  
  356.     while (ok) // atat timp cat mai exista secvente munte le eliminam varfurile
  357.     {
  358.         secventeMunte();
  359.         eliminareVarfuri();
  360.     }
  361.  
  362.     fprintf(fout,"%d\n",k); // punctul b, numarul de secvente obtinute prin eliminarea tuturor varfurilor
  363.     fprintf(fout,"%d\n",n); // punctul c, numarul de elemente ramase in sirul final care nu mai contine secvente munte
  364.  
  365.     return 0;
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement