#include <iostream>
using namespace std;
int forta(int n)
{//calculeaza forta lui n, adica nr de 1 in baza 2
int c=0;
while(n>0)
{
c=c+n%2;
n=n/2;
}
return c;
}
void ordonareforta(int n, int X[])
{//ordonez crescator dupa forta
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
if(forta(X[i])>forta(X[j]))
{
int aux=X[i];
X[i]=X[j];
X[j]=aux;
}
}
void grupuri_de_forta(int n, int X[], int &nrgr, int G[16][101])
{
//in prima coloana retinem nr de valori de pe linia respectiva
for(int i=1;i<=15;i++)
G[i][0]=0;
ordonareforta(n,X);//ordonez dupa forta
nrgr=0;//initializez numarul de grupuri
for(int i=1;i<=n;i++)
if(i==1 || forta(X[i])!=forta(X[i-1]))
{
nrgr++;//grup nou
G[nrgr][0]=1;//e primul
G[nrgr][1]=X[i];//il adaug in contiunare pe linie
}
else
{
G[nrgr][0]++;//crestem nr de valori de pe linie
G[nrgr][G[nrgr][0]]=X[i];//il adaug in contiunare pe linie
}
}
int main()
{
int n=7,A[]={0,12, 3, 24, 16, 15, 32,127},k,G[16][101];
grupuri_de_forta(n,A,k,G);
for(int i=1;i<=k;i++)
{
for(int j=1;j<=G[i][0];j++)
cout<<G[i][j]<<" ";
cout<<endl;
}
return 0;
}
|