[蓝桥杯][历届试题]蚂蚁感冒 (思维)

在这里插入图片描述
思路:这道题没有必要去用模拟写,其实最后的结果和第一只感冒的蚂蚁的头的方向没关系。
只有以下两种情况其它的蚂蚁会感冒:
1.在第一只蚂蚁的右边,并且向左走。
2.在第一只蚂蚁的左边,并且向右走。
今日份水题=-=

#include <bits/stdc++.h>

using namespace std;
int a[55];
int main()
{
    
    
    ios::sync_with_stdio(false);
    int n; cin>>n;
    for(int i=0;i<n;i++){
    
    
        cin>>a[i];
    }
    int cnt = 1;
    for(int i=1;i<n;i++){
    
    
        if(abs(a[0])<abs(a[i])&&a[i]<0){
    
    
            cnt++;
        }
        if(abs(a[0])>abs(a[i])&&a[i]>0){
    
    
            cnt++;
        }
    }
    cout<<cnt<<endl;
}

猜你喜欢

转载自blog.csdn.net/qq_43811879/article/details/107889967