Sadly, the LAST programmer (first to be asked) has a 50/50 shot of living. But on the plus side, if you trust him, everyone else can live! Here's how it works: The last programmer counts the number of red hats in front of him. If it's odd, he says "red". If it's even, he says "blue". The next programmer in line then looks at the number of red hats in front of him. If the person behind him called out "red", and he sees an odd number of red hats, he *knows* his hat is blue, and can call it out. And if he sees an even number of red hats, he *knows* his hat is red. Etc. This proceeds all the way up the line-- and assuming nobody makes a mistake.
The Simple C Program :
int row[]={0,1,0,1,0,0,0,1,0,1, 0,1,0,0,0,0,0,0,0,1, 0,0,0,0,1,0,0,0,0,1, 0,1,0,0,0,0,1,0,0,0, 0,0,0,0,0,1,0,0,1,1, 0,0,0,0,0,0,0,0,0,0, 0,0,0,1,0,0,0,0,0,1, 0,0,0,0,0,0,0,0,0,0, 0,1,0,1,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,1,1};
int Rslt[]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0};
int count=99;
int lIntNumber=0;
for(int i=99;i>0;i--)
{
lIntNumber=0;
for(int j=count;j>0;j--)
{
lIntNumber+=row[j-1]; //lIntNumber^=row[j-1];
}
if(lIntNumber%2==0)
Rslt[i]=0;
else
Rslt[i]=1;
if(i!=99)
{
int lIntPrv=0;
for(int k=i+1;k<100;k
{ lIntPrv+=Rslt[k]; //lIntPrv^=Rslt[k];
}
if(lIntPrv%2==1 && Rslt[i]==1)//OK
Rslt[i]=0;
else if(lIntPrv%2==1 && Rslt[i]==0)
Rslt[i]=1;
else if(lIntPrv%2==0 && Rslt[i]==0)//OK
Rslt[i]=0;
else if(lIntPrv%2==0 && Rslt[i]==1)
Rslt[i]=1;;
}
count--;
}
int lIntRslt=0;
for(int lIntLoop=0;lIntLoop<100;lintloop++)
{
if(Rslt[lIntLoop]==row[lIntLoop])
lIntRslt++;
}
The output most of the times comes 99.
Friday, June 19, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment