zoukankan      html  css  js  c++  java
  • 蚂蚁感冒---第五届蓝桥杯

    蚂蚁感冒

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

     

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

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

        这些蚂蚁中,有1仅仅蚂蚁感冒了。

    而且在和其他蚂蚁碰面时,会把感冒传染给碰到的蚂蚁。

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

    【数据格式】 

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

     

        接着的一行是n个用空格分开的整数 Xi (-100 < Xi < 100), Xi的绝对值,表示蚂蚁离开杆子左边端点的距离。

    正值表示头朝右。负值表示头朝左,数据中不会出现0值,也不会出现两仅仅蚂蚁占用同一位置。当中,第一个数据代表的蚂蚁感冒了。

     

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

    比如。输入:

    3

    5 -2 8

    程序应输出:

    1

     再比如。输入:

    5

    -10 8 -20 12 25

    程序应输出:

    资源约定:

    峰值内存消耗 < 256M

    CPU消耗  < 1000ms 

    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。 

    全部代码放在同一个源文件里。调试通过后,拷贝提交该源代码。

     

    注意: main函数须要返回0

    注意仅仅使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。

    注意全部依赖的函数必须明白地在源文件里 #include <xxx>, 不能通过project设置而省略经常使用头文件。

     

    提交时。注意选择所期望的编译器类型。


    第一种方式:原始方式

                                  

    public class 蚂蚁感冒 {
    	public static void main(String[] args) {
    		int count;// 表示蚂蚁的总和
    		String in;
    		Integer[] julis;
    		Scanner scanner = new Scanner(System.in);
    		count = scanner.nextInt();
    		scanner.nextLine();
    		julis = new Integer[count];// 用来装载详细的每仅仅蚂蚁的距离
    		boolean[] isGanMao = new boolean[count];// 每仅仅蚂蚁是否感冒的详细数据
    		in = scanner.nextLine();
    		long start = System.currentTimeMillis();
    		String[] strings = in.split(" ");
    		for (int i = 0; i < count; i++) {
    			julis[i] = new Integer(strings[i]);
    			isGanMao[i] = false;
    		}// 数据输入完毕
    		isGanMao[0] = true;
    		print(calculate(count, julis, isGanMao)+1);//感染的其它蚂蚁的数量和第一个干嘛的蚂蚁
    		long end = System.currentTimeMillis();
    		print("此程序执行,花费的时间是" + ((end - start) / 1000.0) + "秒.");
    	}
    
    	public static int calculate(int count, Integer[] julis, boolean[] isGanMao) {
    		int ganmao = 0;// 感冒的蚂蚁的数量
    		int likai = 0;// 离开杆子的蚂蚁的数量
    
    		while (likai < count) {
    			for (int i = 0; i < count; i++) {// 对每仅仅蚂蚁的行走计算
    				if (Math.abs(julis[i]) == 0 || Math.abs(julis[i]) == 100) {
    					likai++;
    				}
    				else {
    					// 碰头的交换
    					ganmao += exchange(julis, isGanMao);
    					julis[i]++;// 向左走的继续向左走,向右走得向右
    				}
    			}
    		}
    		return ganmao;
    	}
    	public static boolean isPengTou(Integer A, Integer B) {
    		// 推断是否碰头
    		//if (A * B < 0 && A == B)// 假设方向不同
    		if (A * B < 0 && Math.abs(A)-Math.abs(B)==1)// 假设方向不同
    		{
    			return true;
    		}
    		return false;
    	}
    	public static int exchange(Integer[] julis, boolean[] isGanMao) {
    		int count = 0;
    		// 假设碰头就交换方向,返回感冒传染添加后的数量
    		for (int i = 0; i < julis.length; i++) {
    			for (int j = i + 1; j < julis.length; j++) {
    				if (isPengTou(julis[i], julis[j])) {
    					Integer temp = julis[i];
    					julis[i] = julis[j];
    					julis[j] = temp;
    					if ((isGanMao[j] == true && isGanMao[i] == false)
    							|| (isGanMao[i] == true && isGanMao[j] == false)) {
    						isGanMao[i] = true;// 感冒传染
    						isGanMao[j] = true;
    						count++;
    					}
    
    				}
    			}
    		}
    		return count;
    	}
    	public static void print(Object o) {
    		System.out.println(o.toString());
    	}
    }

    输入和输出结果:

                         5
                         -10 8 -20 12 25
                         3
                         此程序执行,花费的时间是0.001秒.


  • 相关阅读:
    Top 10 Free IT Certification Training Resources
    在线学编程!十大IT在线教育网站推荐
    2016年国际十大科技新闻解读
    2016上半年度私有云提供商排行榜 :华为位居第一
    12 Top Open Source Data Analytics Apps
    Careers/Staffing Index
    top 9 Cloud Computing Failures
    344. Reverse String
    283. Move Zeroes
    DataContract with Json.Net
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/6759296.html
Copyright © 2011-2022 走看看