招商银行信用卡中心笔试
唯一一次编程题都ac的笔试,感恩😂
第一题:
判断4的倍数,和2的倍数的数量。
注意:最终结果要用long(刚开始忘了,通过了35%左右
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); long[] a = new long[n]; long[] b = new long[m]; long res1 = 0; long re1=0; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); if(a[i]==0){ continue; } if(a[i] % 4 ==0){ res1++; }else{ if(a[i] % 2 ==0){ re1++; } } } long res2 = 0; long re2=0; for (int i = 0; i < m; i++) { b[i] = in.nextInt(); if(b[i]==0){ continue; } if(b[i] % 4 ==0){ res2++; }else{ if(b[i] % 2 ==0){ re2++; } } } long res=res1*m+res2*(n-res1); res+=re1*re2; System.out.println(res); } }第二题:
双指针+贪心思想,不断调整距离中心元素绝对值差最大的元素。
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } Arrays.sort(a); int res = 0; int i = 0; int j = n - 1; while (i < j) { if (a[j] - a[i] > k) { res++; int mid = i + (j - i) / 2; if (a[mid] - a[i] > a[j] - a[mid]) { i++; } else { j--; } } else { break; } } System.out.println(res); } }