Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //problema 1 - interclasare
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- // punctul a
- FILE *fin,*fout;
- fin = fopen("date.in","r");
- fout = fopen("date.out","w");
- int a[100],b[100],c[100];
- int i, j, n, m, p = 0;
- fscanf(fin,"%d",&n);
- for (i=0; i<n; i++)
- fscanf(fin,"%d",&a[i]);
- fscanf(fin,"%d",&m);
- for (i=0; i<m; i++)
- fscanf(fin,"%d",&b[i]);
- i=0; j=0;
- while (i < n && j < m)
- if (a[i] < b[j])
- {
- c[p] = a[i];
- p++;
- i++;
- }
- else
- {
- c[p] = b[j];
- p++;
- j++;
- }
- if (i <= n)
- {
- int k;
- for (k = i; k < n; k++)
- {
- c[p] = a[k];
- p++;
- }
- }
- if (j <= m)
- {
- int k;
- for (k = j; k < m; k++)
- {
- c[p] = b[k];
- p++;
- }
- }
- for (i = 0; i < p; i++)
- fprintf(fout,"%d ",c[i]);
- // punctul b
- fprintf(fout,"\n");
- i = p - 1;
- int nr_crt = -1;
- while (i >= 0)
- {
- if ((c[i] == c[i-1]) && (c[i] != nr_crt))
- {
- fprintf(fout,"%d ",c[i]);
- nr_crt = c[i];
- }
- i--;
- }
- return 0;
- }
- //problema 1 punctul c
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- // punctul c
- FILE *fin,*fout;
- fin = fopen("date.in","r");
- fout = fopen("date.out","w");
- int a[100],b[100],c[100];
- int i, j, n, m, p = 0;
- fscanf(fin,"%d",&n);
- for (i=0; i<n; i++)
- fscanf(fin,"%d",&a[i]);
- fscanf(fin,"%d",&m);
- for (i=0; i<m; i++)
- fscanf(fin,"%d",&b[i]);
- i=0; j= m - 1;
- while (i < n && j >= 0)
- if (a[i] < b[j])
- {
- if (a[i] % 2 == 0)
- {
- c[p] = a[i];
- p++;
- }
- i++;
- }
- else
- {
- if (b[j] % 2 == 0)
- {
- c[p] = b[j];
- p++;
- }
- j--;
- }
- if (i <= n)
- {
- int k;
- for (k = i; k < n; k++)
- if (a[k] % 2 == 0)
- {
- c[p] = a[k];
- p++;
- }
- }
- if (j >= 0)
- {
- int k;
- for (k = j; k >= 0; k--)
- if (b[k] % 2 == 0)
- {
- c[p] = b[k];
- p++;
- }
- }
- for (i = 0; i < p; i++)
- fprintf(fout,"%d ",c[i]);
- return 0;
- }
- // problema 2 - paranteze
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- FILE *fin,*fout;
- fin = fopen("date.in","r");
- fout = fopen("date.out","w");
- char s[30];
- int i, c = 0, total = 0;
- fscanf(fin,"%s",s);
- for (i = 0; i < strlen(s); i++)
- {
- if (s[i] == '(')
- { c++; total++; }
- else if (s[i] == ')')
- { c--; total++; }
- if (c < 0)
- {
- fprintf(fout,"Secventa de paranteze este gresita.");
- break;
- }
- }
- if (c == 0 && total)
- fprintf(fout,"Secventa de paranteze este corecta.");
- else if (c > 0)
- fprintf(fout,"Secventa de paranteze este gresita.");
- return 0;
- }
- // problema 2 - punctul b
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- FILE *fin,*fout;
- fin = fopen("date.in","r");
- fout = fopen("date.out","w");
- char s[30];
- int i, c = 0, cd = 0, total = 0, totald = 0, gasit_dr = 0, gasit_ro = 0, ordine_ok = 1;
- fscanf(fin,"%s",s);
- for (i = 0; i < strlen(s); i++)
- {
- if (s[i] == '[')
- { cd++; totald++; gasit_dr = 1; }
- else if (s[i] == ']')
- { cd--; totald++; gasit_dr = 1; }
- if (s[i] == '(')
- { c++; total++; gasit_ro = 1; }
- else if (s[i] == ')')
- { c--; total++; gasit_ro = 1; }
- if (gasit_ro == 1 && gasit_dr == 0)
- {
- ordine_ok = 0;
- fprintf(fout,"Secventa de paranteze este gresita.");
- return 0;
- }
- if (c < 0)
- {
- fprintf(fout,"Secventa de paranteze este gresita.");
- return 0;
- }
- if (cd < 0)
- {
- fprintf(fout,"Secventa de paranteze este gresita.");
- return 0;
- }
- }
- if (ordine_ok == 0)
- { fprintf(fout,"Secventa de paranteze este gresita."); return 0; }
- else if (c > 0)
- { fprintf(fout,"Secventa de paranteze este gresita."); return 0; }
- else if (cd > 0)
- { fprintf(fout,"Secventa de paranteze este gresita."); return 0; }
- else if (c == 0 && total && cd == 0 && totald)
- fprintf(fout,"Secventa de paranteze este corecta.");
- return 0;
- }
- // problema 3
- #include <stdio.h>
- #include <stdlib.h>
- FILE *fin,*fout;
- int v[100], n, ok;
- int varfuri[100], k;
- void citire()
- {
- fin = fopen("date.in","r");
- int i = 0;
- while ( fscanf(fin,"%d",&v[i++]) == 1)
- n = i;
- printf("%d\n",n);
- for (i=0; i<n; i++)
- printf("%d ",v[i]);
- fclose(fin);
- }
- void secventeMunte()
- {
- int i, varf, pozVarf, cresc, descresc;
- ok = 0; //fals
- i = 0;
- while (i < n - 1)
- {
- cresc = descresc = 0;
- while (v[i] < v[i+1] && i < n - 1) // "urcam muntele" pana ajungem la varf
- { i++; cresc++; } // cresc numara secventa munte
- varf = v[i]; pozVarf = i; // memoram valoare si pozitia varfului
- while (v[i] > v[i+1] && i < n -1)
- { i++; descresc++; } // descr numara secventa munte
- if (cresc + descresc >= 2 && cresc > 0 && descresc > 0) // stabilim daca am gasit o secventa munte
- {
- varfuri[k++] = varf; // salvam varfurile intr-un alt vector
- v[pozVarf] = -1; // marcam varfurile in vectorul initial
- ok = 1; // am gasit cel putin o secventa munte
- }
- }
- }
- void eliminareVarfuri()
- {
- int i, k;
- i = 0;
- while (i < n)
- {
- if (v[i] == -1) // daca gasim un varf il eliminam
- {
- for (k = i; k < n - 1; k++)
- v[k] = v[k + 1];
- n--;
- }
- else
- i++;
- }
- }
- int main()
- {
- fout = fopen("date.out","w");
- citire();
- secventeMunte();
- eliminareVarfuri();
- fprintf(fout,"%d\n",k); // punctul a, afisam numarul de secvente munte din vectorul initial
- while (ok) // atat timp cat mai exista secvente munte le eliminam varfurile
- {
- secventeMunte();
- eliminareVarfuri();
- }
- fprintf(fout,"%d\n",k); // punctul b, numarul de secvente obtinute prin eliminarea tuturor varfurilor
- fprintf(fout,"%d\n",n); // punctul c, numarul de elemente ramase in sirul final care nu mai contine secvente munte
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment