首页 > 微众银行5月11日笔试
头像
我要上阿里
编辑于 2020-05-11 21:13
+ 关注

微众银行5月11日笔试

有胖友做了今天的微众笔试吗,求教最后一题思路,这是我前两题的代码
public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int m = scan.nextInt();
		int l = scan.nextInt();
		int r = scan.nextInt();
		int[] a = new int[n];
		int[] b = new int[m];
		for(int i = 0; i < n; i++) a[i] = scan.nextInt();
		for(int i = 0; i < m; i++) b[i] = scan.nextInt();
		Arrays.sort(a);
		Arrays.sort(b);
		int cnt = 0;
		for(int i = 0; i < n; i++) {
			if(a[i] > r) continue;
			for(int j = 0; j < m; j++) {
				if(a[i] + b[j] <= r && a[i] + b[j] >= l) cnt++;
				if(a[i] + b[j] > r) break;
			}
		}
		System.out.print(cnt);
		scan.close();
	}
}

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		long t = scan.nextLong();
		for(long i = 0; i < t; i++) {
			System.out.println(helper(scan.nextLong(), scan.nextLong(), scan.nextLong()));
		}
		scan.close();
	}
	
	static long helper(long a, long b, long x) {
		if(a == x) return Math.abs(x - b);
		if(b == x) return Math.abs(x - a);
		if((a < x && b > x) || (b < x && a > x)) {
			return Math.abs(x - b) + Math.abs(x - a);
		}else {
			if(Math.abs(x - a) <= Math.abs(x - b)) {
				return Math.abs(b - a) + Math.abs(a - x);
			}else {
				return Math.abs(b - a) + Math.abs(b - x);
			}
		}		
	}
}



全部评论

(11) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐