Blue Bridge Cup the previous questions questions ants cold

Blue Bridge cup filled several questions hexagonal previous questions

N-ants 100 cm long straight elongated pole. Their heads and some left, some to the right.
Each ant can creep forward along the pole, speed is 1 cm / sec.
When two ants meet, they will turn around while crawling in the opposite direction.
These ants, the ants have a cold. And when and meet other ants, ant to colds you will encounter.

Please work, when all the ants were crawling away from the pole, how many ants suffering from a cold.
Input
of the first input line of an integer n (1 <n <50) , represents the total number of ants.

Followed by n row is separated by spaces integer Xi (-100 <Xi <100), the absolute value of Xi, ant away from the left end of the pole. Positive values ​​indicate head rightward, leftward head represents a negative value, the data value 0 does not appear, it will not occupy the same position two ants appear. Among them, the ants first data representative of the cold.

Output
required output an integer representing the number of ants final cold.

Sample input
. 5
-10. 8 -20 12 is 25

Sample output
3

When two ants meet, they will turn in the opposite direction while crawling -> I think there can be understood as they pass through each other's body and move on.

We can use an example of drawing analysis, that the ants cold conditions to be satisfied

  1. The initial cold front go with ants, ants that they were relatively cold and the line will
  2. Behind the cold ant (behind here is relative, if cold ants climb to the right, then he's left behind in the cold, said ants) and it is saying the same direction and the line of ants might catch a cold.

Why is it possible flu, because it can be cold necessarily be saying ant relatively cold and the line of the infection, but simply asked if the line relative, it must with an initial relatively cold ant the line, however, is not necessarily there will be ants with ants initially relatively cold and the line, so to determine whether there is an initial cold with ants and ant opposite row.

#include <iostream>
using namespace std;
int main()
{
	int n, a[55], i, sum = 1, temp;
	cin >> n;
	for (i = 0 ; i < n ; i++)
	{
		cin >> a[i];
	}
	temp = a[0];//记录初始感冒蚂蚁
	if (temp >= 0)//初始感冒蚂蚁朝右走 
	{
		for (i = 0 ; i < n ; i++)
		{
			if (a[i] < 0 && -a[i] > temp)//在初始感冒蚂蚁的右边并且与其相对而行,即满足第一个条件
			{
				sum++;
			}
		}
		if (sum != 1)//判断第二个条件能否继续进行,即是否有与初始感冒蚂蚁相对而行的蚂蚁
		{
			for (i = 0 ; i < n ; i++)
			{
				if (a[i] > 0 && a[i] < temp)//在初始感冒蚂蚁的左边并且与其同向而行,即满足第二个条件
				{
					sum++;
				}
			}
		}
	}
	else
	{
		for (i = 0 ; i < n ; i++)
		{
			if (a[i] > 0 && a[i] < -temp)//在初始感冒蚂蚁的左边并且与其相对而行,即满足第一个条件
			{
				sum++;
			}
		}
		if (sum != 1)
		{
			for (i = 0 ; i < n ; i++)
			{
				if (a[i] < 0 && a[i] < temp)//在初始感冒蚂蚁的右边并且与其同向而行,即满足第二个条件
				{
					sum++;
				}
			}
		}
	}
	cout << sum;
}

If wrong, point out trouble! Thanks for watching!

Published 13 original articles · won praise 5 · Views 490

Guess you like

Origin blog.csdn.net/qq_44410340/article/details/104969448