码迷,mamicode.com
首页 > 编程语言 > 详细

201809-2 买菜 Java

时间:2020-02-27 13:03:16      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:util   重叠   ++   png   new   inf   imp   int()   判断   

技术图片

思路:
顺序读入,例如:小H装车的时间段为【1,3】,小W装车的时间段为【2,4】,重叠部分为【2,3】,记在数组times[2]中。最后输出时判断数组times中值大于1的(其实就是2),即为重叠部分

import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] times = new int[1000000];
        int count = 0;
        for(int i = 0; i < 2*n; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            for(int j = a; j < b; j++) {
                times[j]++;
            }
        }
        for(int i : times) 
            if(i > 1) 
                count++;
        System.out.println(count);
    }
 
}

201809-2 买菜 Java

标签:util   重叠   ++   png   new   inf   imp   int()   判断   

原文地址:https://www.cnblogs.com/yu-jiawei/p/12371415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!