nbut [1173] Birdlike Angry Pig 找出某个数使得等于其它所有数的&操作

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
    
  • 提示
  • 来源
  • XadillaX

     

    题意  :    输入n  之后n个数 其中可能存在一个数是另外n-1个数的and 即&操作的结果 如果存在一个这样数 输出他 否则输出CAUTION: NO BIRDLIKE
     
    思路:
    对于第i个数  只要验证 其前面的所有数的and 和后面所有数的and的结果进行and操作后 看结果是否等于第i个数
    是则输出他     
    我们求出 1->n的连续的数的and   存到数组a里    求出n->1连续的数的and  存进b数组  按照上面思路找即可  详细 看代码    代码一看就明白了
     
    #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;
    }

  • 你可能感兴趣的:(nbut [1173] Birdlike Angry Pig 找出某个数使得等于其它所有数的&操作)