微众银行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); } } } }