当前位置:网站首页>Past blue bridge cup test questions ants catch cold

Past blue bridge cup test questions ants catch cold

2022-06-28 19:49:00 Yuyy

This paper is finally updated at 1174 Days ago, , The information may have developed or changed .

Problem description

Long 100 Cm long and thin straight pole n Only ants . Some of their heads are to the left , Some of them face right. .

Every ant can only climb along the pole , The velocity is 1 centimeter / second .

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

Among these ants , Yes 1 Only ants have a cold . And when we meet other ants , Can spread cold to ants .

Please calculate , When all the ants get off the pole , How many ants have a cold .

Input format

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

The next line is n Integers separated by spaces Xi (-100 < Xi < 100), Xi The absolute value of , Represents the distance of the ant from the left end of the pole . A positive value means head right , Negative means head left , Not in data 0 value , And there will be no two ants occupying the same place . among , The first data represents that the ant has a cold .

Output format

Request output 1 It's an integer , Indicates the number of last cold ants . The sample input 3 5 -2 8 Sample output 1 The sample input 5 -10 8 -20 12 25 Sample output 3

Tips

When two ants meet , They will turn around and crawl in the opposite direction at the same time . It can be understood as crossing the shoulder , The effect is the same .

Code

using namespace std;
int main(){
	int array[55];
	int n,flag,flag1=0,flag2=0,temp,sum=1;
	cin>>n;
	cin>>flag;
	for(int i=1;i<n;i++){
		cin>>array[i];
		//cin>>temp;
		//array[i]=temp;		
	}
	if(flag>0){
		for(int i=1;i<n;i++){			
			if(array[i]<0&&(flag+array[i])<0){
				sum++;
				flag1=1;
			}
		}
		if(flag1==1){
			for(int i=1;i<n;i++){			
				if(array[i]>0&&(flag-array[i])>0){
					sum++;					
				}
			}
		}
			
	}
	else{
		for(int i=1;i<n;i++){			
			if(array[i]>0&&(flag+array[i])<0){
				sum++;
				flag2=1;
			}
		}
		if(flag2==1){
			for(int i=1;i<n;i++){			
				if(array[i]<0&&(flag-array[i])>0){
					sum++;					
				}
			}
		}
	}
	cout<<sum;
	return 0;
}

Post Views: 266

原网站

版权声明
本文为[Yuyy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/179/202206281927478135.html