1. Se da un vector cu nr intregi cu valori distincte. Se cere sa afisati toate posibilitatile de a alege k din cele n citite astfel incat oricare doua nr alaturate sa fie de paritati diferite.
#include<iostream>1.II.
using namespace std;
int a[10];
int n,x[10],nrsol,k;
int afisare()
{int i;
nrsol++;
for(i=1;i<=k;i++)
cout<<a[x[i]]<<" ";
cout<<endl;
}
int verif(int i)
{int j;
for(j=1;j<i;j++)
if(x[j]==x[i] || a[x[i]]%2==a[x[i-1]]%2 && i>1)
return 0;
return 1;
}
void back(int i)
{int j;
for(j=1;j<=n;j++)
{x[i]=j;
if(verif(i))
if(i==k)
afisare();
else back(i+1);
}
}
int main()
{int i;
cout<<"n=";cin>>n;
cout<<"k=";cin>>k;
for(i=1;i<=n;i++)
cin>>a[i];
back(1);
cout<<endl<<"Nr. sol= "<<nrsol;
}
#include<iostream>2. Se citesc n culori. Sa se formeze toate drapelele de 3 culori astfel incat oricare doua culori alaturate sa fie distincte.
using namespace std;
int a[10];
int n,x[10],nrsol,k;
int afisare()
{int i;
nrsol++;
for(i=1;i<=k;i++)
cout<<x[i]<<" ";
cout<<endl;
}
int verif(int i)
{int j;
for(j=1;j<i;j++)
if(x[j]==x[i] || x[i]%2==x[i-1]%2 && i>1)
return 0;
return 1;
}
void back(int i)
{int j;
for(j=1;j<=n;j++)
{x[i]=a[j];
if(verif(i))
if(i==k)
afisare();
else back(i+1);
}
}
int main()
{int i;
cout<<"n=";cin>>n;
cout<<"k=";cin>>k;
for(i=1;i<=n;i++)
cin>>a[i];
back(1);
cout<<endl<<"Nr. sol= "<<nrsol;
}
#include<iostream>
using namespace std;
char a[10][10];
int n,x[10],nrsol,k;
int afisare()
{int i;
nrsol++;
for(i=1;i<=k;i++)
cout<<a[x[i]]<<" ";
cout<<endl;
}
int verif(int i)
{int j;
for(j=1;j<i;j++)
if(x[j]==x[i] || x[i]==x[i-1] && i>1)
return 0;
return 1;
}
void back(int i)
{int j;
for(j=1;j<=n;j++)
{x[i]=j;
if(verif(i))
if(i==k)
afisare();
else back(i+1);
}
}
int main()
{int i;
cout<<"n=";cin>>n;
k=3;
for(i=1;i<=n;i++)
cin>>a[i];
back(1);
cout<<endl<<"Nr. sol= "<<nrsol;
}