2024年春季学期《算法分析与设计》练习4

文章介绍了两种搜索算法,deferfen函数演示了二分查找的应用,而sanfen函数展示了三元查找。它们在处理整数列表时用于查找特定目标值的位置。同时,还涉及到其他编程技术,如列表操作和递归函数。
摘要由CSDN通过智能技术生成

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)

仔细一点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值