5 条题解
-
1
using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int t; cin >> t; while (t-- > 0) { int x; cin >> x; int l = 0, r = n - 1; while (l <= r) { int m = l + (r - l) / 2; if (a[m] <= x) { l = m + 1; } else { r = m - 1; } } if (r >= 0 && a[r] == x) { cout << r + 1 << " "; } else { cout << -1 << " "; } } return 0; }
-
0
歪解 哈希表加快读
import java.util.*; import java.io.*; public class Main { static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) throws Exception{ int n = Int(); HashMap<Integer,Integer> hm = new HashMap<>(); for(int i = 1; i<= n;i++) { hm.put(Int(),i); } int t = Int(); for(int i = 1; i<= t;i++) { pw.print(hm.getOrDefault(Int(), -1)+" "); } pw.flush(); } public static int Int() throws Exception{ st.nextToken(); return (int)(st.nval); } }
-
0
import java.io.*; import java.util.*; import java.lang.*; import java.util.regex.*; import java.math.*; public class Main { static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static List<List<Integer>>res=new ArrayList<>(); static int n,m; public static void main(String[] args) throws Exception { Scanner sc=new Scanner(System.in); n=Int(); var arr=new int[n+1]; for (int i = 1; i <= n; i++) { arr[i]=Int(); } m=Int(); for (int i = 1; i <= m; i++) { int x=Int(); pw.print( check(arr,x)+" "); } pw.flush(); } public static int check(int[]arr,int x){ var l=1; var r=n; while (l<r){ int mid=(l+r)/2; if(arr[mid]>=x)r=mid; else l=mid+1; } if(arr[l]!=x) return -1; else { while (arr[l+1]==arr[l]) l++; return l; }} public static String Line() throws Exception{ String s = bf.readLine(); return s; } public static int Int() throws Exception{ st.nextToken(); return (int) st.nval; } public static long Long() throws Exception{ st.nextToken(); return (long) st.nval; } public static double Double() throws Exception{ st.nextToken(); return (double)st.nval; } }
-
0
import java.io.*; import java.util.Arrays; public class Main { static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter bw = new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws Exception { int n = Int(); int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) { arr[i] = Int(); } int t = Int(); //循环查找每一个需要查找的元素 for (int i = 0; i < t; i++) { int key = Int(); //调用二分查找算法,返回查找结果并输出到输出流中 int res = binarySearch0(arr, key); bw.print((res + 1) + " "); } //刷新输出流 bw.flush(); } //读入整数的方法 public static int Int() throws Exception{ st.nextToken(); return (int)(st.nval); } //二分查找算法 public static int binarySearch0(int[] a, int key) { int low = 0; int high = a.length - 1; while (low <= high) { int mid = (low + high) / 2; int midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else { //进行左右判断,找到第一个等于key的元素并返回 low = mid; if(((mid >0 && mid < a.length - 1) && a[mid - 1] != key && a[mid + 1] != key) || (mid == 0 && a[mid + 1] != key) || (mid == a.length - 1 && a[mid - 1] != key)) return mid; //如果不是第一个等于key的元素,继续查找下一个元素直到最后一个等于key的元素并返回 while (low <= high) { mid = (low + high) / 2; midVal = a[mid]; if(midVal != key) high = mid - 1; else low = mid + 1; } return low - 1; } } //未找到等于key的元素 return -2; } }
-
0
public class Main{ public static void main(String args[]){ Scanner in=new Scanner(System.in); int n=in.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) { a[i]=in.nextInt(); } int t=in.nextInt(); while(t-->0) { int x=in.nextInt(); int l=0,r=n-1; while(l<=r) { int m=l+(r-l)/2; if(a[m]<=x) { l=m+1; }else { r=m-1; } } if(r>=0&&a[r]==x) { System.out.print(r+1+" "); }else { System.out.print(-1+" "); } } } }
- 1
信息
- ID
- 576
- 时间
- 1000ms
- 内存
- 64MiB
- 难度
- 8
- 标签
- (无)
- 递交数
- 74
- 已通过
- 12
- 上传者