nbut [1173] Birdlike Angry Pig 找出某个数使得等于其它所有数的&操作
2013-04-28 08:13:03

There's no end to revenge.
Pigs like to eat birds' eggs, so birds decide to revenge.
For this, pigs decide to revenge as well.
Pigs give a task to a birdlike pig. They let he sneak into the birds group so that they can destroy the birds group.

Every bird has a ID number (It may not be unique). To let pigs know, the birdlike pig makes the "AND operation" amount all the birds' ID number and sign himself to the answer.

For example:
Birds are signed as 5, 3, 4. So the birdlike pig signs himself as (5 & 3 & 4 = 0).
One day, a group birds pass by pigs' home. So the angry pig want to know whether the birdlike pig is in.

This problem has several cases. The first line of each case is an integer N (2 <= N <= 100 000).
Then follows a line with N integers, indicates the ID of each bird/birdlike pig.

For each case, if you can find the birdlike pig then output the ID of birdlike pig. Or output 'CAUTION: NO BIRDLIKE'.

5
4 0 3 4 5

0

[cpp]
#include<stdio.h>
int a[211111],b[211111],c[211111];  int main()
{      int n,num,i,flag;
while(scanf("%d",&n)!=EOF)
{          flag=0;
scanf("%d",&c[1]);
a[1]=c[1];
for(i=2;i<=n;i++)
{                 scanf("%d",&c[i]);
a[i]=a[i-1]&c[i];              }
b[n]=c[n];
for(i=n-1;i>=1;i--)
{                 b[i]=b[i+1]&c[i];             }
for(i=2;i<=n-1;i++)
{                 if(c[i]==(a[i-1]&b[i+1]))//注意a[i-1]&b[i+1]  一定要用括号括起来 否则运算顺序就变了
{                       printf("%d\n",c[i]);
flag=1;
break;                 }             }
if(flag==1) continue;
if(c[n]==a[n-1])
{                 printf("%d\n",c[n]);
flag=1;
continue;             }
if(flag==1) continue;
if(c[1]==b[2])             {
printf("%d\n",c[1]);
flag=1;                 continue;
}               printf("CAUTION: NO BIRDLIKE\n");
}      return 0;  }  #include<stdio.h>
int a[211111],b[211111],c[211111];
int main()
{
int n,num,i,flag;
while(scanf("%d",&n)!=EOF)
{
flag=0;
scanf("%d",&c[1]);
a[1]=c[1];
for(i=2;i<=n;i++)
{
scanf("%d",&c[i]);
a[i]=a[i-1]&c[i];
}
b[n]=c[n];
for(i=n-1;i>=1;i--)
{
b[i]=b[i+1]&c[i];
}
for(i=2;i<=n-1;i++)
{
if(c[i]==(a[i-1]&b[i+1]))//注意a[i-1]&b[i+1]  一定要用括号括起来 否则运算顺序就变了
{
printf("%d\n",c[i]);
flag=1;
break;
}
}
if(flag==1) continue;
if(c[n]==a[n-1])
{
printf("%d\n",c[n]);
flag=1;
continue;
}
if(flag==1) continue;
if(c[1]==b[2])
{
printf("%d\n",c[1]);
flag=1;
continue;
}
printf("CAUTION: NO BIRDLIKE\n");
}
return 0;