• 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);
    	}
     
    }
    
  • 相关阅读:
    寒假学习进度7
    寒假学习进度3
    寒假学习进度6
    寒假学习进度5
    寒假学习进度8
    加分项
    每日博客
    每日博客
    每日博客
    每日博客
  • 原文地址:https://www.cnblogs.com/yu-jiawei/p/12371415.html
Copyright © 2020-2023  润新知