程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> nbut [1173] Birdlike Angry Pig 找出某個數使得等於其它所有數的&操作

nbut [1173] Birdlike Angry Pig 找出某個數使得等於其它所有數的&操作

編輯:C++入門知識

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;
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved