[蓝桥杯][Previous Test Questions] Ants catch a cold (thinking)

Insert picture description here
Idea: There is no need to use simulation to write this question. In fact, the final result has nothing to do with the direction of the first ant's head.
There are only two situations where other ants will catch a cold:
1. Be on the right side of the first ant and walk to the left.
2. Go to the left of the first ant and go to the right.
Today's water question=-=

#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;
}

Guess you like

Origin blog.csdn.net/qq_43811879/article/details/107889967