Ants Cold
Descriptionthere are n ants on a slender straight pole 100 centimeters long. They have their heads on the left, some facing to the right. Each ant can only climb along the pole, at a speed of 1 centimeters per second. When two ants meet, they will turn around at the same time and crawl in the opposite direction. Of these ants, 1 ants have a cold. And when they meet with other ants, they will infect the ant with the cold. Please calculate how many ants have a cold when all the ants are climbing away from the pole. Input
The first line enters an integer n (1 < n < 50), which represents the total number of ants.
The next line is n a space-separated integer XI ( -100 < Xi <), the absolute value of Xi, indicating the distance the ant leaves the left end of the pole. A positive value indicates a head to the right, a negative value indicates a head to the left, no 0 values appear in the data, and no two ants occupy the same position. Among them, the first data represents the ants catching a cold.
Outputrequires an output of 1 integers, indicating the number of last cold ants. Sample Input
35-2 8
Sample Output
1
isn't there a train of thought? The first time to do this problem, really no train of thought. But at the beginning of Rujia's training guide, two ants meet and then move on, looking from a distance as if they were wearing each other. If you think like that, it's easy. Just determine how many of the ants on the left side of the cold to climb to the right, in judging how many of the right to the left to climb, add up is the answer. Don't forget that you are also a cold ant. The code is as follows:
#include <cstdio>int main () { int n,x,f[110]={0},t,sum=1; scanf ("%d", &n); for (int i=0;i<n;i++) { scanf ("%d", &x); if (i==0) x>0?t=x:t=-x; x>0?f[x]=1:f[-x]=-1; } for (int i=0;i<t;i++) if (f[i]==1) sum++; for (int i=t+1;i<110;i++) if (f[i]==-1) sum++; printf ("%d\n", sum); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sdnu 1206. Ants Cold "code so simple, thinking practice" "July 29"