百度笔试 前端
我也不知道为什么,两个本地A,提交就0.0,跪求大佬们能不能帮我看看为什么。泪崩😥
第二题:
import java.util.Scanner; public class Main { static int find (int t,int []a){ //类似二分查找,找到刚好大一点的下标 int be=0;int end=a.length-1; while(be<=end){ int inter=(be+end)>>1; if(t<a[inter]){ if(inter-1>be&&a[inter-1]>t) end=inter-1; else { return inter+1; } } else if(t>a[inter]){ be=inter+1; }else return inter+1; } return -1; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while (T > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int[] N = new int[n]; int[] M = new int[m]; for (int i = 0; i < n; i++) { N[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { M[i] = sc.nextInt(); } //Array.sort(M) //排序 for (int i = 0; i < n; i++) { System.out.print(find(N[i], M)); if (i < n - 1) System.out.print(" "); } System.out.println(); T--; } } }
第三题:优质奶牛区间
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while (T > 0) { int n=sc.nextInt(); int m=sc.nextInt(); int a[]=new int[n]; for(int t=0;t<m;t++){ int k=sc.nextInt(); for(int i=0;i<k;i++){ int be=sc.nextInt(); int end=sc.nextInt(); while(be<=end){ a[be]++; be++; } } } StringBuilder s=new StringBuilder(); int count=0; for(int i=0;i<n;i++){ if(a[i]==m){ count++; s.append(i); if(i+1<n) s.append(" "); } } System.out.println(count); System.out.println(s.toString()); T--; } } }