[蓝桥杯][历届试题]蚂蚁感冒(模拟全过程)

在线测试连接

**

题目描述

**
长100厘米的细长直杆子上有n只蚂蚁。它们的头有的朝左,有的朝右。

每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒。

当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行。

这些蚂蚁中,有1只蚂蚁感冒了。并且在和其它蚂蚁碰面时,会把感冒传染给碰到的蚂蚁。

请你计算,当所有蚂蚁都爬离杆子时,有多少只蚂蚁患上了感冒。

输入

第一行输入一个整数n (1 < n < 50), 表示蚂蚁的总数。

接着的一行是n个用空格分开的整数 Xi (-100 < Xi < 100), Xi的绝对值,表示蚂蚁离开杆子左边端点的距离。正值表示头朝右,负值表示头朝左,数据中不会出现0值,也不会出现两只蚂蚁占用同一位置。其中,第一个数 据代表的蚂蚁感冒了。

输出

要求输出1个整数,表示最后感冒蚂蚁的数目。

样例输入

5
-10 8 -20 12 25

样例输出

3

解题思路及代码

import java.util.*;
/*
 * 蚂蚁类
 * 
 * **/
class Ant{
	public int dir;//运动方向有1和-1两个取值
	public int pos;//位置
	public boolean is = false;//是否感冒
	public Ant(int x) {
		super();
		if(x > 0) {
		this.dir = 1;
		this.pos = x ;
		}
		else {
			this.dir = -1;
			this.pos = -x ;
		}
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + dir;
		result = prime * result + pos;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Ant other = (Ant) obj;
		if (dir != other.dir)
			return false;
		if (pos != other.pos)
			return false;
		return true;
	}
	
	
}
public class antCold {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int count = 1;//刚开始感冒的蚂蚁数为1
		int n = sc.nextInt();
		ArrayList ant = new ArrayList(); //数组表存放所有运动的蚂蚁  (用容器类有利于实现动态管理)
		for(int i = 0; i < n;++i){
			//数据输入
			if(i!=0)
			ant.add(new Ant(sc.nextInt()));
			else {
				//第一个蚂蚁应特殊处理
				Ant temp = new Ant(sc.nextInt());
				temp.is =true;//给蚂蚁的感冒标记变量设置为true
				ant.add(temp);
			}
		}
		while (ant.isEmpty() == false) {//大循环  直至ant为空
			for(int i = 0;i <ant.size();++i) {//第一层循环
				//每个蚂蚁的位置更新,更新完再第二层循环寻找碰面的蚂蚁
				Ant temp1 = (Ant)ant.get(i);//取出来
				temp1.pos += temp1.dir*1;//位置更新
				if(Math.abs(temp1.pos) >=100) {//蚂蚁已经走出规定区域,删掉就好
					ant.remove(i);//删除
					i--;//尤其注意,删除之后 后面的元素自动补进来所以 索引i应该回退一个
					continue;//蚂蚁已经删除了  不需要寻找有没有碰面的了
				}
				else {
					ant.set(i,temp1);//更新蚂蚁的数据
				}
				for (int j = 0;j <ant.size();++j) {//第二层循环   遍历一遍ant 寻找有没有碰面的
					if(i == j)continue;//通过自己
					Ant temp2 = (Ant)ant.get(i);
					Ant temp3 = (Ant)ant.get(j);
					if(temp2.pos == temp3.pos&&temp2.dir!=temp3.dir) {//匹配碰面的蚂蚁
						temp2.dir = -temp2.dir;//运动方向反向
						temp3.dir = -temp3.dir;//运动方向反向
						if(temp2.is == true&&temp3.is == true)continue;//二者都感冒了  无需操作
						if(temp2.is == false&&temp3.is == false)continue;//二者都没感冒  无需操作
						else {//其中有一个感冒了
							//设置两个蚂蚁都感冒
							temp2.is = true;
							temp3.is = true;
							//计数器加一
							count ++;
						}
						//更新两个蚂蚁的数据
						ant.set(i, temp2);
						ant.set(j, temp3);
					}

				}
			}
		}
		System.out.println(count);
	}

}

Attention:为了解题方便Ant类成员变量全部声明为public,这将破坏对象的封装,实际写代码时不要养成这样的坏习惯!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值