18-09-2(买菜)

简单的分情况讨论

import java.util.*;
public class Main{
    
    
	public static void main(String[] args) {
    
    
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		int time =0;
		int[][] h = new int[n][2];
		int[][] w = new int[n][2];
		for(int p =0;p<n;p++) {
    
    
		h[p][0] = input.nextInt();
		h[p][1] = input.nextInt();
	}
		for(int p =0;p<n;p++) {
    
    
		w[p][0] = input.nextInt();
		w[p][1] = input.nextInt();
	}
		input.close();
		int i =0;
		int j =0;
		while(i<n&&j<n) {
    
    
			int start_h = h[i][0];
			int end_h = h[i][1];
			int start_w = w[j][0];
			int end_w = w[j][1];
//			System.out.print(start_h+" "+end_h+" ");
//			System.out.println(start_w+" "+end_w);
			// 1
			if(start_w >= start_h && start_w < end_h) {
    
    
				if(end_w >= end_h) {
    
    
					time += end_h-start_w;
					i++;
					continue;
				}
				if(end_w < end_h) {
    
    
					time += end_w-start_w;
					j++;
					continue;
				}				
			}
			//2
			if(start_h >= start_w && start_h < end_w) {
    
    
				if(end_h >= end_w) {
    
    
					time += end_w-start_h;
					j++;
					continue;
				}
				if(end_h < end_w) {
    
    
					time += end_h-start_h;
					i++;
					continue;
				}				
			}
			
			if(start_h >= end_w) j++;
			if(start_w >= end_h) i++;
		
			//System.out.print(i+" "+j);
			
		}
		System.out.print(time);
		
	}


}

猜你喜欢

转载自blog.csdn.net/qq_51985653/article/details/121439546