题目分析
这道题分析了一下得出的结论就是最后感冒的蚂蚁数量等于绝对值比第一个感冒的蚂蚁的位置大的且方向向左,还有就是比第一个感冒的蚂蚁的位置小的且方向向右。(这里我们很明显可以发现2个蚂蚁碰面之后并没有什么影响,2个速度相同的蚂蚁同时掉头不如直接看成速度相同的蚂蚁继续向前走)
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int val[55];
int cmp(int a, int b){
return abs(a) < abs(b);
}
int main(){
int n;
while(scanf("%d", &n) != EOF){
for(int i = 0; i < n; i++) scanf("%d", &val[i]);
int temp = val[0];
sort(val, val+n, cmp);
int ans = 1;
for(int i = 0; i < n; i++){
if(val[i] == temp){
for(int j = 0; j < i; j++)
if(val[j] > 0) ans++;
for(int j = i+1; j < n; j++)
if(val[j] < 0) ans++;
break;
}
}
printf("%d\n", ans);
}
return 0;
}