def erfen(arr, target, left, right):
if left > right:
return -1
mid = left + (right - left) // 2
if arr[mid] == target:
return mid + 1
elif target < arr[mid]:
return erfen(arr, target, left, mid - 1)
else:
return erfen(arr, target, mid + 1, right)
while True:
arr = list(map(int, input().split()))
n = arr[0]
exp = arr[1:-1]
ind = arr[-1]
l, h = 0, n - 1
print((erfen(exp, ind, l, h)))
def erfen(arr, target, left, right):
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid+1
if target < arr[mid]:
right = mid - 1
elif target > arr[mid]:
left = mid + 1
return -1
while True:
arr = list(map(int, input().split()))
n = arr[0]
exp = arr[1:-1]
ind = arr[-1]
l, h = 0, n - 1
print((erfen(exp, ind, l, h)))
#include <iostream>
using namespace std;
int erfen(int arr[], int target, int left, int right) {
if (left > right) {
return -1;
}
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid + 1;
}
else if (target < arr[mid]) {
return erfen(arr, target, left, mid - 1);
}
else {
return erfen(arr, target, mid + 1, right);
}
}
int main() {
int n, q;
while (cin >> n) {
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cin >> q;
int arr_q[q];
for (int i = 0; i < q; i++) {
cin >> arr_q[i];
}
int ans = 0;
for (int i = 0; i < q; i++) {
if (erfen(arr, arr_q[i], 0, n - 1) != -1) {
ans++;
}
}
cout << ans << endl;
}
return 0;
}
python过不了,时间空间都要暴
def partition(arr, p, q):
i = p
x = arr[p]
for j in range(p + 1, q + 1):
if arr[j] <= x:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[p], arr[i] = arr[i], arr[p]
return i
def qsort(arr, p, q):
if p < q:
r = partition(arr, p, q)
qsort(arr, p, r - 1)
qsort(arr, r + 1, q)
return arr
while True:
exp = list(map(int, input().split()))
arr = exp[1:]
print(*(qsort(arr, 0, len(arr) - 1)))
def sanfen(arr, target, left, right):
while left <= right:
mid1 = left + (right - left) // 3
mid2 = right - (right - left) // 3
if arr[mid1] == target:
return [mid1 + 1, mid1 + 1]
if arr[mid2] == target:
return [mid2 + 1, mid2 + 1]
if target < arr[mid1]:
right = mid1 - 1
elif target > arr[mid2]:
left = mid2 + 1
else:
left = mid1 + 1
right = mid2 - 1
return [right + 1, left + 1]
while True:
arr = list(map(int, input().split()))
n = arr[0]
exp = arr[1:-1]
ind = arr[-1]
l, h = 0, n - 1
print(*(sanfen(exp, ind, l, h)))
三分查找
t = int(input())
for i in range(t):
y = int(input())
start, end = 0.00, 100.00
while abs(start - end) > 1e-7:
mid = (start + end) / 2
if 42 * mid ** 6 + 48 * mid ** 5 + 21 * mid ** 2 + 10 * mid < y:
start = mid
else:
end = mid
result = 6 * mid ** 7 + 8 * mid ** 6 + 7 * mid ** 3 + 5 * mid ** 2 - y * mid
print("%.4f" % result)
仔细一点