201809-2 买菜 Java

思路:
顺序读入,例如:小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);
	}
 
}
原文地址:https://www.cnblogs.com/yu-jiawei/p/12371415.html