Sei sulla pagina 1di 32

Stanciulescu Alexandra

10. La un spectacol trebuie sa interpreteze cate o poezie copiii A, B, C, D, E astfel incat copilul D sa
recite inainte de A si B. Sa se genereze toate posibilitatile de recitare a poeziilor. #include<iostream.h> #include<fstream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,m,i,x[100],k,nr,j,p,a; int cont1() {nr=0; for(i=1;i<=3;i++) if(x[i]==4){nr=0; for(j=i+1;j<=5;j++)if(x[j]==1||x[j]==2)nr++; if(nr==2)return 1; else return 0; } } int cont(int k) {for(i=1;i<k;i++)if(x[i]==x[k])return 0; return 1; } void afis() {if(cont1()) {for(i=1;i<=k;i++) {if(x[i]==1)g<<"A "; if(x[i]==2)g<<"B "; if(x[i]==3)g<<"C "; if(x[i]==4)g<<"D "; if(x[i]==5)g<<"E "; } g<<'\n';} } void back() {k=1;x[k]=0; do{while(x[k]<5){x[k]++; if(cont(k))if(k==5)afis(); else {k++;x[k]=0;} } k--; } while(k>0); }

Stanciulescu Alexandra
int main() {back();f.close();g.close();return 0; } 11.Intre n persoane care stau pe scaune s-au iscat conflicte. Acestea stau pe scaune numerotate de la 1 la n. Scrieti un program care sa afiseze toate modurile posibile de reasezare a persoanelor astfel incat sa nu se gaseasca alaturi doua persoane in conflict. //1-nu se dusmanesc 0-se dusmanesc. #include<iostream.h> #include<fstream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,m,i,x[100],k,a[100][100],j; void citire() {f>>n; for(i=1;i<=n;i++) for(j=1;j<=n;j++)f>>a[i][j]; } int cont(int k) {for(i=1;i<k;i++)if(x[i]==x[k])return 0; return 1;} int cont1(int k) {for(i=1;i<n;i++)if(a[x[i]][x[i+1]]==0||a[x[i+1]][x[i]]==0)return 0; return 1;} void afis() {if(cont1(k)){for(i=1;i<=n;i++)g<<x[i]<<' '; g<<'\n';} } void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(cont(k))if(k==n)afis(); else {k++;x[k]=0;} } k--;} while(k>0);} int main() {citire(); back(); f.close();g.close();return 0;} Exemplu:n=7 si matricea de dusmanie: 1011111

Stanciulescu Alexandra
1111111 0111011 1101111 1 1 1 1 1 0 1 Se va afisa 1 4 5 7 3 2 6, etc. 12.N copii au fost asezati in sir indian. Se cunoaste configuratia initiala. Sa se reaseze copiii astfel incat fiecare copil sa se situeze intre alti copii, diferiti de cei din configuratia initiala. #include<iostream.h> #include<fstream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,m,i,x[100],k,v[100]; void citire() {f>>n; for(i=1;i<=n;i++)f>>v[i]; } int cont1() {for(i=2;i<=n;i++)if(v[x[i]-1]==v[x[i-1]]||v[x[i]+1]==v[x[i+1]])return 0; return 1; } void afis() {if(cont1()){for(i=1;i<=n;i++)g<<v[x[i]]<<' '; g<<'\n';} } int cont(int k) {for(i=1;i<k;i++)if(x[i]==x[k])return 0; return 1; } void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(cont(k))if(k==n)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire(); back();

Stanciulescu Alexandra
f.close();g.close();return 0; } Exemplu:n=6 si configuratia initiala 1 2 3 4 5 6 132465 132654 135264 13.N copii se aseaza in sir indian. Se cunosc numele celor n copii. Sa se gaseasca toate posibilitatile de aranjare in sir astfel incat un baiat sa urmeze dupa cel mult doua fete alaturate. //daca( x[i]%2==0) inseamna ca x[i] e fata. #include<iostream.h> #include<fstream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,m,i,x[100],k; void citire() {f>>n; } int cont1() {for(i=1;i<n-1;i++)if(x[i]%2==0&&x[i+1]%2==0&&x[i+2]%2==0)return 0; return 1; } void afis() {if(cont1()){for(i=1;i<=n;i++)g<<x[i]<<' '; g<<'\n';} } int cont(int k) {for(i=1;i<k;i++)if(x[i]==x[k])return 0; return 1; } void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(cont(k))if(k==n)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main()

Stanciulescu Alexandra
{citire(); back(); f.close();g.close();return 0; } Exemplu:n=3 123654 124356 124365 14.N copii se aseaza in cerc. Se cunosc numele celor n copii. Sa se gaseasca toate posibilitatile de rearanjare in cerc. #include<fstream.h> #include<iostream.h> #include<string.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int x[100],k,n,i; char v[100][20]; void citire() {f>>n; for(i=1;i<=n;i++){f.get();f.get(v[i],20);} } int cont(int k) {for(i=1;i<k;i++)if(x[i]==x[k])return 0; return 1; } void afis() {for(i=1;i<=n;i++)g<<v[x[i]]<<' '; g<<'\n'; } void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(cont(k))if(k==n)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main()

Stanciulescu Alexandra
{citire();back(); f.close();g.close();return 0; }

15.Se cer toate solutiile de asezare in linie a m caini si n pisici astfel incat sa nu existe o pisica intre doi caini #include<fstream.h> #include<iostream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int x[100],k,n,i,m,n1,n2; void citire() {f>>m>>n; } int cont(int k) {for(i=1;i<k-;i++)if(x[i]==1&&x[i+1]==2&&x[i+2]==1)return 0; return 1; } int cont1(int k) {for(i=1;i<=k;i++)if(x[i]==1)n1++;else n2++; if(n1==m&&n2==n)return 1; else return 0; } void afis() {if(cont1(k)){for(i=1;i<=k;i++)g<<x[i]<<' '; g<<'\n';} } void back() {k=1;x[k]=0; do{while(x[k]<2){x[k]++; if(cont(k))if(k==m+n)afis(); else {k++;x[k]=0;} }

Stanciulescu Alexandra
k--; } while(k>0); } int main() {citire();back(); f.close();g.close();return 0;}

16.Gigel are n cartonase (n<=10). Pe fiecare este scrisa o cifra de la 1 la 9. Uilizand doua tipuri de cartonase cu + si - vrea sa obtina rezultatul 2. Care sunt solutiile pentru n citit? #include<iostream.h> #include<fstream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,x[100],k,i,s,nr,v[100]; void citire() {f>>n; for(i=1;i<=n;i++)f>>v[i]; } int cont(int k) {s=0; for(i=1;i<=k;i++)if(x[i]%2==1)s=s+v[x[i]]; else s=s-v[x[i]]; if(s==2)return 1; else return 0; } void afis() {if(cont(k)) {s=0; for(i=1;i<=k;i++)g<<v[x[i]]<<' '; nr++; g<<'\n';} } int cont1(int k) {for(i=1;i<k;i++)if(x[i]==x[k])return 0;return 1;}

Stanciulescu Alexandra
void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(k==2)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();back(); if(nr==0)g<<"nu exista solutii";f.close();g.close();return 0; }

17.Sa se aranjeze in toate modurile elementele unui vector a*1+,a*2+a*n+ formand secvente de lungime p, astfel incat fiecare element sa apara de cel mult doua ori #include<iostream.h> #include<fstream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,x[100],i,k,p,a[100]; void citire() {f>>n>>p; for(i=1;i<=n;i++)f>>a[i]; } void afis() {for(i=1;i<=p;i++)g<<a[x[i]]<<' '; g<<'\n'; } int cont(int k) {for(i=1;i<k-1;i++)if(x[i]==x[k])return 0; return 1; } void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(cont(k))if(k==p)afis(); else {k++;x[k]=0;}

Stanciulescu Alexandra
} k--; } while(k>0); } int main() {citire();back();f.close();g.close();return 0; }

18.Fie sirul primelor n numare naturale (n citit de la tastatura). Sa se insereze inainte de fiecare semnul + sau minus. Pentru fiecare solutie astfel generata se va afisa valoarea expresiei. Ex pt n =3: +1+2+3=6 +1+2-3=0 +1-2+3=2 etc #include<iostream.h> #include<fstream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,x[100],k,i,s; void citire() {f>>n; } void afis() {s=0; for(i=1;i<=n;i++) {if(x[i]==1){g<<'+';s=s+i;} else {g<<'-';s=s-i;} g<<i; } g<<'='<<s; g<<'\n'; } void back() {k=1;x[k]=0;

Stanciulescu Alexandra
do{while(x[k]<2){x[k]++; if(k==n)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();back();f.close();g.close();return 0; }

19.La o cofetarie se comercializeaza n sortimente de prajituri. Sa se determine toate variantele de a face pachete cu cate p prajituri diferite. Scrieti un program care permite citirea de la tastatura a celor n sortimente de prajituri si afiseaza variantele solutie precum si numarul acestora. #include<iostream.h> #include<fstream.h> #include<string.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,x[100],p,k,i,s,nr; char v[100][20]; void citire() {f>>n>>p; for(i=1;i<=n+1;i++){f.get();f.get(v[i],20);} } void afis() {nr++; for(i=1;i<=p;i++)g<<v[x[i]]<<' '; g<<'\n'; } int cont(int k) {for(i=1;i<k;i++)if(x[i]==x[k])return 0; return 1; }

Stanciulescu Alexandra
void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(cont(k))if(k==p)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();back(); f.close();g.close();return 0; }

20.La o masa rotunda sunt n persoane de diverse nationalitati, pentru fiecare persoana precizandu-se doua limbi straine cunoscute de ea. Se cere sa ajutati organizatorii mesei rotunde sa aranjeze persoanele astfel incat fiecare sa poata conversa atat cu cea din stanga cat si cu cea din dreapta. #include<iostream.h> #include<fstream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,x[100],i,k; struct pers{int n,l1,l2;}; pers v[100]; void citire() {f>>n; for(i=1;i<=n;i++)f>>v[i].n>>v[i].l1>>v[i].l2; } void afis() {for(i=1;i<=n;i++)g<<v[x[i]].n<<' '<<v[x[i]].l1<<' '<<v[x[i]].l2<<'\n'; g<<'\n'; } int cont(int k) {for(i=1;i<k;i++) {if(x[i]==x[k]||(v[x[k]].l1!=v[x[k-1]].l1&&v[x[k]].l1!=v[x[k-1]].l2&&v[x[k]].l2!=v[x[k-1]].l1

Stanciulescu Alexandra
&&v[x[k]].l2!=v[x[k-1]].l2))return 0; if(k==n)if(v[x[1]].l1!=v[x[n]].l1&&v[x[1]].l1!=v[x[n]].l2&&v[x[1]].l2!=v[x[n]].l1&&v[x[1]].l2!=v[x[n]].l2) return 0;} return 1; } void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(cont(k))if(k==n)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();back(); f.close();g.close();return 0; }

21.Teste la geografie. Pentru lucrarea de control profesoara de geografie a pregatit n teste. In clasa sunt p elevi (p>n). Sa se genereze toate posibilitatile de a imparti testele celor p elevi astfel incat fiecare test sa fie rezolvat de macar un elev. #include<iostream.h> #include<fstream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,x[100],p,k,i,j,ok; void citire() {f>>n>>p; } void afis() {for(i=1;i<=p;i++)g<<x[i]<<' '; g<<'\n'; } int cont(int k) {for(i=1;i<=n;i++) {ok=1; for(j=1;j<=k;j++)

Stanciulescu Alexandra
if(i==x[j]){ok=0;break;} if(ok)return 0; } return 1; } void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(cont(k))if(k==p)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();back(); f.close();g.close();return 0; }

22.Sa se genereze toate drapelele tricolore care se pot forma cu n culori #include<iostream.h> #include<fstream.h> #include<string.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,x[100],p,k,i,j,ok; char v[100]; void citire() {f>>n; for(i=1;i<=n;i++)f>>v[i]; } void afis() {for(i=1;i<=n;i++)g<<v[x[i]]<<' '; g<<'\n'; } int cont(int k)

Stanciulescu Alexandra
{for(i=1;i<k;i++)if(x[i]==x[k])return 0; return 1; } void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(cont(k))if(k==n)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();back(); f.close();g.close();return 0; } Exemplu:n=3, R G A Solutie: RGA RAG GRA GAR ARG AGR 23.Produsul cartezian a n multimi impunand conditia ca suma elementelor dintr-o solutie sa fie egala cu un S citit . #include<iostream.h> #include<fstream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,x[100],p,k,S,s,i,j,v[100]; void citire() {f>>n>>S; for(i=1;i<=n;i++)f>>v[i]; } int cont(int k) {s=0; for(i=1;i<=k;i++)s+=x[i]; if(s==S)return 1;

Stanciulescu Alexandra
else return 0; } void afis() {if(cont(n)){for(i=1;i<=n;i++)g<<x[i]<<' '; g<<'\n';} } void back() {k=1;x[k]=0; do{while(x[k]<v[k]){x[k]++; if(k==n)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();back(); f.close();g.close();return 0; } Exemplu:n=3; S=6, si multimile 3,2,4 Solutia: 114 123 213 222 312 24.Sa se genereze toate submultimile de cate k elemente care se pot forma cu numerele 1,2n (sau a*1+,a*2+a*n+), cu conditia ca fiecare element sa fie divizibil cu un numar d dat. #include<iostream.h> #include<fstream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,x[100],p,k,d,K,i; void citire() {f>>n>>K>>d; } int cont(int k) {for(i=1;i<=k;i++)if(x[i]%d!=0)return 0; return 1;

Stanciulescu Alexandra
} void afis() {for(i=1;i<=k;i++)g<<x[i]<<','; g<<'\n'; } void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(cont(k))if(k==K)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();back(); f.close();g.close();return 0; } Exemplu:n=6,K=3;d=2; Solutie: 2,2,2 2,2,4 2,2,6 2,4,2 2,4,4 2,4,6 2,6,2 25.La un festival de muzica usoara s-au inscris n melodii codificate cu numere de la 1 la n. Stiind ca in prima zi intra in concurs k melodii, sa se afiseze toate posibilitatile de a stabili ordinea intrarii in concurs a melodiilor in prima zi, stiind ca melodiile de coduri c1 si c2 trebuie sa intre in prima zi, a doua respectiv penultima #include<fstream.h> #include<iostream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int n,x[100],k,i,K,c1,c2; void citire() {f>>n>>K>>c1>>c2; }

Stanciulescu Alexandra
void afis() {g<<c1<<' '<<c2<<' '; for(i=1;i<=k;i++)g<<x[i]<<' '; g<<'\n'; } int cont(int k) {int nr=0; for(i=1;i<k;i++)if(x[i]==x[k])return 0; return 1; } void back() {k=1;x[k]=2; do{while(x[k]<n){x[k]++; if(cont(k))if(k==K-2)afis(); else {k++;x[k]=2;} } k--; } while(k>0); } int main() {citire();back();f.close();g.close();return 0; }

26.Sa se afiseze toate numerele de lungime p<=9 cu proprietatea ca au suma cifrelor egala cu x dat #include<fstream.h> #include<iostream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int p,x[100],k,i,X,c1,c2; void citire() {f>>p>>X; } int cont(int k)

Stanciulescu Alexandra
{int nr=0;if(x[1]==0)return 0; for(i=1;i<=k;i++)nr=nr+x[i]; if(nr==X)return 1; else return 0; } void afis() {if(cont(k)){for(i=1;i<=k;i++)g<<x[i]; g<<'\n'; } } void back() {k=1;x[k]=-1; do{while(x[k]<9){x[k]++; if(k==p)afis(); else {k++;x[k]=-1;} } k--; } while(k>0); } int main() {citire();back();f.close();g.close();return 0; } Exemplu: p=3, x=6 105 114 123 132 141 150 204 213 etc. 27.Sa se afiseze toate submultimile cu p elemente dintre elementele a*1+,a*2+a*n+ cu proprietatea ca suma elementelor din multime este un numar divizibil cu x dat #include<fstream.h> #include<iostream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int p,x[100],k,n,i,X,a[100]; void citire()

Stanciulescu Alexandra
{f>>n>>p>>X; for(i=1;i<=n;i++)f>>a[i]; } int cont(int k) {int nr=0;if(x[1]==0)return 0; for(i=1;i<=k;i++)nr=nr+a[x[i]]; if(nr==X)return 1; else return 0; } void afis() {if(cont(k)){for(i=1;i<=k;i++)g<<a[x[i]]; g<<'\n'; } } void back() {k=1;x[k]=0; do{while(x[k]<9){x[k]++; if(k==p)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();back();f.close();g.close();return 0; } Exemplu:n=9,p=3,x=6 123456789 Va afisa: 114 123 132 141 etc. 28.Sa se afiseze toate modurile in care se poate forma un grup de p persoane dintr-un grup de n persoane, dintre care l persoane sa fie femei #include<fstream.h> #include<iostream.h> ifstream f("intrare.in"); ofstream g("iesire.out");

Stanciulescu Alexandra
int p,x[100],k,n,i,nr,l; void citire() {f>>n>>p; } int cont(int k) {nr=0; for(i=1;i<=k;i++)if(x[i]==2)nr++; if(nr==l)return 1; else return 0; } void afis() {if(cont(k)){for(i=1;i<=k;i++)g<<x[i]<<' '; g<<'\n';} } void back() {k=1;x[k]=0; do{while(x[k]<2){x[k]++; if(k==p)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();back();f.close();g.close();return 0; } Exemplu: n=10,p=6,l=2 Va afisa: 111122 111212 111221 112112 112121 112211 1 2 1 1 1 2 etc. 29.La un concurs se prezinta n concurenti din m tari. Sa se stabileasca ordinea intrarii in concurs a celor n concurenti astfel incat doi concurenti din aceeasi tara sa nu urmeze unul dupa altul #include<fstream.h> #include<iostream.h>

Stanciulescu Alexandra
ifstream f("intrare.in"); ofstream g("iesire.out"); int x[100],k,n,i,nr,m,c[100]; void citire() {f>>n>>m; for(i=1;i<=n;i++)f>>c[i]; } int cont1() {for(i=1;i<n;i++)if(c[x[i]]==c[x[i+1]])return 0; return 1; } void afis() {if(cont1()){for(i=1;i<=k;i++)g<<c[x[i]]<<' '; g<<'\n'; } } int cont(int k) {for(i=1;i<k;i++)if(x[i]==x[k])return 0; return 1; } void back() {k=1;x[k]=0; do{while(x[k]<n){x[k]++; if(cont(k))if(k==n)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();back();f.close();g.close();return 0; } Exemplu: n=5,m=3; si concurentii din tarile: 1 1 2 3 1 Va afisa: 12131 12131 1 3 1 2 1 etc. 30.Sa se aranjeze elementele multimii {A,R,G,V} in grupuri de cate n (n par) astfel incat doua caractere identice sa nu fie alaturate si R sa apara de exact n/2 ori

Stanciulescu Alexandra
#include<fstream.h> #include<iostream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int x[100],k,n,i,nr; void citire() {f>>n; } int cont1() {nr=0; for(i=1;i<=n;i++)if(x[i]==2)nr++; if(nr==n/2)return 1; else return 0; } void afis() {if(cont1()) {for(i=1;i<=k;i++){if(x[i]==1)g<<'A'; if(x[i]==2)g<<'R'; if(x[i]==3)g<<'G'; if(x[i]==4)g<<'V'; } g<<'\n'; } } int cont(int k) {for(i=1;i<k;i++)if(x[i]==x[i+1])return 0; return 1; } void back() {k=1;x[k]=0; do{while(x[k]<4){x[k]++; if(cont(k))if(k==n)afis(); else {k++;x[k]=0;} } k--; } while(k>0); } int main() {citire();if(n%2==0)back();else g<<"N nu este par";f.close();g.close();return 0;}

Stanciulescu Alexandra
31.Sa se genereze toate numerele de lungime n formate doar cu cifre pare/impare . #include<fstream.h> #include<iostream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int x[100],k,n,i; void citire() {f>>n; } void afis() {for(i=1;i<=n;i++)g<<x[i]<<' '; g<<'\n'; } int cont(int k) {for(i=1;i<=k;i++)if(x[i]%2!=0)return 0; return 1; } void back() {k=1;x[k]=0; do{while(x[k]<9){x[k]++; if(cont(k))if(k==n)afis(); else {k++;x[k]=-1;} } k--; } while(k>0); } int main() {citire();back(); f.close();g.close();return 0; } Exemplu:n=4; 2000 2002 2004 2006 2008 2020 2022 2024

Stanciulescu Alexandra
2026 2 0 2 8 etc. 32.Sa se genereze toate numerele de lungime n divizibile cu x dat . #include<fstream.h> #include<iostream.h> ifstream f("intrare.in"); ofstream g("iesire.out"); int x[100],k,n,i,nr,X; void citire() {f>>n>>X; } int cont(int k) {nr=0; for(i=1;i<=k;i++)nr=nr*10+x[i]; if(nr%X==0)return 1; else return 0; } void afis() {if(cont(k)){for(i=1;i<=n;i++)g<<x[i]; g<<'\n';} } void back() {k=1;x[k]=0; do{while(x[k]<9){x[k]++; if(k==n)afis(); else {k++;x[k]=-1;} } k--; } while(k>0); } int main() {citire();back(); f.close();g.close();return 0; } Exemplu:n=3, X=10. Va afisa: 100 110 120

Stanciulescu Alexandra
130 140 150 160 etc.

Stanciulescu Alexandra

Stanciulescu Alexandra

1.O tabla de sah se citeste ca o matrice n*n in care pozitiile libere au valoarea 0, iar piesele sunt marcate
prin valoarea 1. Pe prima linie pe coloana js se afla un pion. Sa se determine drumul pe care poate ajunge pionul pe ultima linie luand un numar maxim de piese. Pozitia initiala a pionului se considera libera. Pionul aflat in pozitia i,j se poate deplasa astfel: - in pozitia i+1,j daca e libera - in pozitia i+1, j-1 daca e piesa in aceasta pozitie - in pozitia i+1, j+1 daca e piesa in aceasta pozitie Exemplu: 53 00000 01010 01111 00011 01011 Drumul optim este: 13 22 33 44 55 Pe acest drum pionul ia 4 piese. #include<fstream.h> ifstream f("pion.in"); ofstream g("pion.out"); int n,i,j, a[50][50], c[50][50],b[50][50],is,js; void citire() { int i,j;

Stanciulescu Alexandra
f>>n>>js; is=1; for(i=1;i<=n;i++) for(j=1;j<=n;j++) f>>a[i][j]; } void pd() { int i,j; b[is][js]=1; for(i=2;i<=n;i++) { for(j=1;j<=n;j++) { if(a[i][j]==0) { if(b[i-1][j]>0) { c[i][j]=c[i-1][j]; b[i][j]=1; } } else if(c[i-1][j-1]>c[i-1][j+1]) { if(b[i-1][j-1]>0) {c[i][j]=c[i-1][j-1]+1;b[i][j]=1; } else if(b[i-1][j+1]>0) {c[i][j]=c[i-1][j+1]+1;b[i][j]=1; } } else { if(b[i-1][j+1]>0) {c[i][j]=c[i-1][j+1]+1;b[i][j]=1; } else if(b[i-1][j-1]>0) {c[i][j]=c[i-1][j-1]+1;b[i][j]=1; } } } } } void drum(int i, int j) { if(i==1) fout<<i<<" "<<j<<endl; else { if(a[i][j]==0) drum(i-1,j); else if(c[i-1][j-1]+1==c[i][j]) drum(i-1,j-1); else drum(i-1,j+1); g<<i<<" "<<j<<endl; } } void afis() { int max=0,jm; for(j=1;j<=n;j++) if(c[n][j]>max) { max=c[n][j]; jm=j; }

Stanciulescu Alexandra
g<<max<<endl; drum(n,jm); } int main() { citire();pd();afis(); f.close(); g.close(); return 0; }

2.Subsir crescator maximal.


Se citeste un numar n si apoi un sir de n numere intregi. Gasiti cel mai lung subsir al sirului citit care are proprietatea ca elementele sunt in ordine crescatoare. Daca exista mai multe subsiruri de lungime maxima se va afisa unul dintre ele. Exemplu: date.in 9 423052698 date.out 23569 #include <fstream.h> ifstream f("date.in"); ofstream g("date.out"); int n, A[10000], L[10000], maxt, pm; void afis(int k, int m) { if(m>0) { int i=k-1; while(L[i]!=m-1) i--; afis(i,m-1); g<<A[k]<<" "; } } int main() { int i,j,maxx; f>>n; for(i=1;i<=n;i++) fin>>A[i]; L[1]=1; for(i=2;i<=n;i++) {

Stanciulescu Alexandra
maxx=0; for(j=1;j<i;j++) if(A[j]<=A[i] && L[j]>maxx) maxx=L[j]; L[i]=maxx+1; if(L[i]>maxt) { maxt=L[i]; pm=i; } } afis(pm,maxt); return 0;}

3.Calculati cate submultimi cu k elemente are o multime cu n elemente.


#include <fstream.h> ifstream f ("date.in"); ofstream g("date.out"); int main() { unsigned long int a[500][500]={0}; int i,j,k,n; f>>n>>k; a[0][0]=1; for(i=1;i<=n;i++) for(j=0;j<=i;j++) if(j==0 || j==i) a[i][j]=1; else a[i][j]=a[i-1][j]+a[i-1][j-1]; g<<a[n][k]<<endl; f.close(); g.close(); return 0;}

Stanciulescu Alexandra

4.In fisierul m.in se afla pe prima linie un numar natural n cel mult 20000,iar pe liniile urmatoare o
matrice patratica de dimensiune n care contine doar elemente 0 si 1. Sa se determine cel mai mare patrat care contine doar valori 1. Se for afisa in fisierul text m.out urmatoarele valori separate prin spatiu: latura patratului, linia si coloana coltului stanga sus al patratului. Daca exista mai mult astfel de patrate se va afisa doar cel mai de sus. Exemplu: 5 11000 11101 11111 11100 10111 Se afiseaza 3 2 1 #include<fstream.h> int n, a[3][20001],maxx,im,jm; ifstream f("m.in"); ofstream g("m.out"); void Read(); void Write(); int main() { Read(); Write(); f.close(); g.close(); return 0; }

Stanciulescu Alexandra
void Read() { int i,j,m,x; f>>n; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { f>>x; if(i==1) a[i][j]=x; else { if(x==0) a[2][j]=0; else if(j==1) a[2][j]=x; else { m=a[1][j-1]; if(a[1][j]<m) m=a[1][j]; if(a[2][j-1]<m) m=a[2][j-1]; a[2][j]=m+1; if(a[2][j]>maxx) { maxx=a[2][j]; im=i; jm=j; } } } a[1][j-1]=a[2][j-1]; } a[1][n]=a[2][n]; } } void Write() { g<<maxx<<" "<<im-maxx+1<<" "<<jm-maxx+1; }