#include <iostream>
using namespace std;
int search(int *b, int n, int q) {
int s = 1, e = n;
while(s<=e&&e<=n&&s>=1) {
int m = (s + e) / 2;
if (b[m] >= q) {
if (b[m-1] < q) {
return m;
} else {
e = m - 1;
}
} else {
if (b[m+1] >= q) {
return m + 1;
} else {
s = m + 1;
}
}
}
return -1;
}
int main(int argc, char**argv) {
int n, m, a, *b, q;
cin>>n;
b = new int[n+2];
b[0] = 0;
b[n+1] = 0;
for (int i = 0; i < n; i++) {
cin>>a;
b[i+1] = b[i] + a;
}
cin>>m;
for (int i = 0; i < m; i++) {
cin>>q;
cout<<search(b, n, q)<<endl;
}
delete []b;
return 0;
}
二分法
最新推荐文章于 2024-08-21 21:50:12 发布