zoukankan      html  css  js  c++  java
  • 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
    样例输出
    3

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Scanner;
    
    public class Main {
        public static int n;
        public static ArrayList<point> num;
        public static int count = 1;
        
        static class point {
            public int position;
            public int id;  
            
            public point(int position, int id) {
                this.position = position;
                this.id = id;
            }
        }
        
        class MyComparator implements Comparator<point> {
            public int compare(point arg0, point arg1) {
                int a = Math.abs(arg0.position);
                int b = Math.abs(arg1.position);
                if(a > b)
                    return 1;
                else if(a < b)
                    return -1;
                return 0;
            }
        }
        
        public void getResult() {
            int judge = num.get(0).position;
            Collections.sort(num, new MyComparator());
            int mid = 0;
            for(int i = 0;i < num.size();i++) {
                if(num.get(i).id == 0) {
                    mid = i;
                    break;
                }
            }
            int leftMin = 0, leftMax = 0, rightMin = 0, rightMax = 0;
            for(int i = 0;i < mid;i++) {
                if(num.get(i).position < 0)
                    leftMin++;
                else if(num.get(i).position > 0)
                    leftMax++;
            }
            for(int i = mid + 1;i < n;i++) {
                if(num.get(i).position < 0)
                    rightMin++;
                else if(num.get(i).position > 0)
                    rightMax++;
            }
            for(int i = 0;i < mid;i++) {
                if(judge > 0 && rightMin > 0 && num.get(i).position > 0)
                    count++;
                else if(judge < 0 && num.get(i).position > 0)
                    count++;
            }
            for(int i = mid + 1;i < n;i++) {
                if(judge > 0 && num.get(i).position < 0)
                    count++;
                else if(judge < 0 && leftMax > 0 && num.get(i).position < 0)
                    count++;
            }
         }
        
        
        public static void main(String[] args) {
            Main test = new Main();
            Scanner in = new Scanner(System.in);
            n = in.nextInt();
            num = new ArrayList<point>();
            for(int i = 0;i < n;i++) {
                int a = in.nextInt();
                num.add(new point(a, i));
            }
            test.getResult();
            System.out.println(count);
        }
    }
    
  • 相关阅读:
    VMware的三种网络连接方式区别
    迁移至博客园
    Oracle常用语句集合
    Oracle表的几种连接方式
    Oracle逻辑结构(TableSpace→Segment→Extent→Block)
    从线性代数的角度理解线性时不变系统和信号响应
    从线性代数的角度理解傅里叶变换
    在WPF中调用文件夹浏览/选择对话框
    在WPF中调用打开文件对话框
    在WPF中调用另存为对话框
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947854.html
Copyright © 2011-2022 走看看