NOJ [1173] Birdlike Angry Pig

通过算法解决了一群愤怒的小鸟和猪之间的复仇问题。利用位运算找出混入鸟群中的特务,采用“AND”操作来识别特务的身份。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 问题描述
  • 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'.


    构造一个数Ai=bird[1]&bird[2]&.....&bird[i-1]&bird[i+1]&......bird[n],
    与乘积分析类似,将Ai分为2部分,分别用数组C,D表示
    则C[i]=C[i-1]&bird[i],D[i]=D[i+1]&bird[i+1 ]

    #include<stdio.h>
    #include<string.h>

    int bird[100010];
    int first[100010];
    int last[100010];
    int main()
    {
    int n;
    while(~scanf("%d",&n))
    {
    for(int i=1;i<=n;i++)
    {
    scanf("%d",&bird[i]);
    if(i==1)
    first[1]=bird[1];
    else
    first[i]=first[i-1]&bird[i];
    }
    for(int i=n;i>=1;i--)
    if(i==n)
    last[n]=bird[n];
    else
    last[i]=(last[i+1]&bird[i]);
    bool p=false;
    for(int i=2;i<=n-1;i++)
    {
    if((first[i-1]&last[i+1])==bird[i])
    {
    printf("%d\n",bird[i]);
    p=true;
    break;
    }
    }
    if(p)
    continue;
    else
    {
    if(bird[1] == last[2])
    printf("%d\n",bird[1] );
    else if(bird[n] == first[n-1])
    printf("%d\n",bird[n] );
    else
    printf("CAUTION: NO BIRDLIKE\n");
    }
    }
    return 0;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值