Blue Bridge] [ant cup cold (simple thinking title)

Title Description

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.

Entry

A first line of input 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.

Export

A request output integer representing the number of ants final cold.

Sample input

5
-10 8 -20 12 25

Sample Output

3

Problem-solving ideas

Simple simulation times will be the outcome of ants speed and the length of the branches are given confusing information, if the ants do not meet it is not contagious.
If ants are too cold to go to the left, depending on the number of only the right to go left ant ants A
(1) If a> 0, then the result is the number of +1 a + only left to go to the right ant ants
( 2) If a = 0, the result is 1
if ants are colds right away, depending on the number of the right to go left ant ant B
(1) if b> 0, then the result is only b + + 1s go right to the left ant ants
(2) if b = 0, then the result is 1

Code

package 蚂蚁感冒;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		
		int []a=new int [50];
		a[1]=in.nextInt();
		int x1=0,x2=0,y1=0,y2=0;
		for(int i=2;i<=n;i++)
		{
			a[i]=in.nextInt();
			if(Math.abs(a[i])<Math.abs(a[1]))
			{
				if(a[i]<0)
				{
					y1++;
				}
				else
				{
					x1++;
				}
			}
			else
			{
				if(a[i]<0)
				{
					y2++;
				}
				else
				{
					x2++;
				}
			}
		}
		if(a[1]<0)
		{
			if(x1>0)
				System.out.println(x1+y2+1);
			else
				System.out.println(x1+1);
		}
		else
		{
			if(y2>0)
				System.out.println(x1+y2+1);
			else
				System.out.println(y2+1);

		}
	}

}

Published 20 original articles · won praise 0 · Views 3860

Guess you like

Origin blog.csdn.net/weixin_44544406/article/details/104282360