-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question2.java
57 lines (48 loc) · 1.5 KB
/
Question2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.li.wangYi;
import java.io.InputStream;
import java.util.Scanner;
/**
* @program: GradleTestUseSubModule
* @author: Yafei Li
* @create: 2018-08-10 18:53
* 网易考试:笔试
**/
public class Question2 {
public static void main(String[] args){
Class clazz = Question2.class.getClass();
InputStream ins = clazz.getResourceAsStream("/month9day16/wangYi/question2.txt");
Scanner scanner = new Scanner(ins);
int n=scanner.nextInt();
int[] duiNums = new int[n]; //堆数
for (int i = 0; i < n; i++) {
duiNums[i]=scanner.nextInt();
}
int askNum = scanner.nextInt(); //询问次数
int[] sumarr = new int[n];
int sum=0;
for (int i = 0; i < n; i++) {
sum = sum + duiNums[i];
sumarr[i]=sum;
}
for (int i = 0; i < askNum; i++) {
int temp = scanner.nextInt();
//二分查找
int j=binarySearch(temp, sumarr,0,sumarr.length-1);
System.out.println(j+1);
}
}
private static int binarySearch(int temp, int[] sumarr,int low,int high) {
if (low >= high) {
return high;
}
int middle=(low+high)/2;
int value = sumarr[middle];
if (value > temp) {
return binarySearch(temp,sumarr,low, middle);
}else if(value<temp){
return binarySearch(temp, sumarr, middle + 1, high);
}else {
return middle;
}
}
}