频道栏目
首页 > 程序开发 > 软件开发 > C++ > 正文
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
提示
无来源
XadillaX
 
题意  :    输入n  之后n个数 其中可能存在一个数是另外n-1个数的and 即&操作的结果 如果存在一个这样数 输出他 否则输出CAUTION: NO BIRDLIKE 思路:对于第i个数  只要验证 其前面的所有数的and 和后面所有数的and的结果进行and操作后 看结果是否等于第i个数是则输出他     我们求出 1->n的连续的数的and   存到数组a里    求出n->1连续的数的and  存进b数组  按照上面思路找即可  详细 看代码    代码一看就明白了
 [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;
 

点击复制链接 与好友分享!回本站首页
上一篇:深入理解Aireplay-ng各种攻击及其原理
下一篇:一个有问题的驱动程序
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站