[蓝桥杯][历届试题]蚂蚁感冒

分情况讨论即可。

以感染蚂蚁向右走为例,对每一个蚂蚁的走向:

  1. 右边向左走,必然被感染
  2. 右边向右走,必然不会被感染
  3. 左边向左走,必然不会被感染
  4. 左边向右走:
    • 右边存在向左走,则必然被感染
    • 右边不存在向左走,则必然不会被感染
const int N=55;
int d[N];
int n;

int main()
{
    cin>>n;

    for(int i=0;i<n;i++) cin>>d[i];

    int res=1;
    if(d[0] < 0)
    {
        for(int i=0;i<n;i++)
            if(d[i] > 0 && d[i] < abs(d[0]))
                res++;
        
        if(res > 1)
        {
            for(int i=0;i<n;i++)
                if(d[i] < 0 && abs(d[i]) > abs(d[0]))
                    res++;
        }
    }
    else
    {
        for(int i=0;i<n;i++)
            if(d[i] < 0 && abs(d[i]) > d[0])
                res++;
                
        if(res > 1)
        {
            for(int i=0;i<n;i++)
                if(d[i] > 0 && d[i] < d[0])
                    res++;
        }
    }
    cout<<res<<endl;
    //system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/fxh0707/p/14571392.html