[Blue Bridge Cup] Exam Questions Training 2014 C++A Group Question 7 Ants catch a cold

Ants catch a cold

There are n ants on a 100 cm long slender and straight pole. Some of their heads are facing left and some are facing right.

Each ant can only climb forward along the pole at a speed of 1 cm/sec.

When two ants meet, they will turn around and crawl in opposite directions at the same time.

Among these ants, one ant caught a cold. And when you meet with other ants, you will spread a cold to the ants you encounter.

Please calculate how many ants caught a cold when all the ants climbed away from the pole.

enter

Enter an integer n (1 <n <50) in the first line, which represents the total number of ants. 

The next line is n integers Xi (-100 <Xi <100) separated by spaces. The absolute value of Xi represents the distance between the ant and the left end of the pole. A positive value indicates that the head is facing right, and a negative value indicates that the head is facing left. There will be no zero value in the data, and no two ants occupying the same position. Among them, the ant represented by the first data has caught a cold.

Output

It is required to output an integer, which represents the number of ants that caught a cold at the end. 

Sample input


-10 8 -20 12 25 
Sample output

3

 

Problem analysis

Instead of simulating a U-turn after encountering, it uses two traversals. After encountering a cold, it is equivalent to two ants passing through each other and moving forward. It does not affect each other, but the number of ants with a cold increases, and when the second time When traversing, it is mainly to consider the direction of the ant that was caught with a cold for the first time and the ant facing it. To put it more bluntly, I am the ant that caught a cold. There is an ant that is coming forward in front of me. Then I will definitely infect that ant. After we meet, we continue to move forward. Later, if we still encounter a coming ant. Ants also pass the cold to him, this is after the first loop traversal, each time the ants are infected with a cold, add one. Then, when the total number of cold ants is not 1, it means that I just met other ants on the road and they also caught a cold, so consider those ants who have been infected with a cold at this time, the same reasoning

#include <iostream>
#include <math.h>
using namespace std;

int main(int argc, char** argv) {
	int n;
	cin >> n;
	int a[n];
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	int x = a[0];
	
	if(x > 0){	//感冒蚂蚁向右 
		int res = 1;
		for(int i = 0; i < n; i++){
			if(a[i] < 0 && -a[i] > x){	//遇上从右向左的蚂蚁 感冒+1  -a[i] - x > 0
				res ++;
			}
		}
		if(res != 1){	//有从右向左的蚂蚁 
			for (int i = 0; i < n; i++){
				if(a[i] > 0 && a[i] < x){
					res ++;
				}
			}
		}
		cout << res << endl;
	}
	
	if(x < 0){ //感冒蚂蚁向左 
		int res = 1;
		for(int i = 0; i < n; i++){
			if(a[i] > 0 && a[i] < -x){	// -x - a[i]> 0
				res ++; 
			}
		}
		if(res != 1){
			for(int i = 0 ; i < n; i++){
				if(a[i] < 0 && -a[i] > -x){	//a[i] > -x -a[i] - -x > 0
					res ++;
				}
			}
		}
		cout << res << endl;
	}

	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/115263247