最长题解
2025-10-07 19:39:30
发布于:广东
15阅读
0回复
0点赞
此题题解
这道题是小学二年级学的二分查找
本题解没有废话真的
这是题解
#include<bits/stdc++.h>
#define MAX_LEN 1000000
/* 安全读取一整行 */
int safe_getline(char *buf, int sz) {
    if (!fgets(buf, sz, stdin)) return -1;
    int len = strlen(buf);
    if (len && buf[len - 1] == '\n') buf[--len] = '\0';
    return len;
}
/* 手工实现一个动态数组 */
typedef struct {
    int *data;
    size_t size;
    size_t cap;
} Vec;
void vec_init(Vec *v) {
    v->cap = 8;
    v->size = 0;
    v->data = (int *)malloc(sizeof(int) * v->cap);
}
void vec_push(Vec *v, int val) {
    if (v->size == v->cap) {
        v->cap <<= 1;
        v->data = (int *)realloc(v->data, sizeof(int) * v->cap);
    }
    v->data[v->size++] = val;
}
void vec_free(Vec *v) {
    free(v->data);
    v->data = NULL;
    v->size = v->cap = 0;
}
/* 手工实现二分查找 */
int binary_search(const int *a, size_t n, int key) {
    size_t l = 0, r = n;
    while (l < r) {
        size_t m = l + ((r - l) >> 1);
        if (a[m] < key) l = m + 1;
        else r = m;
    }
    return (l < n && a[l] == key) ? (int)l : -1;
}
/* 把字符串解析成整数序列 */
void parse_csv(const char *s, Vec *out) {
    const char *p = s;
    while (*p) {
        while (isspace((unsigned char)*p)) ++p;
        if (*p == ',') { ++p; continue; }
        char *endptr;
        long v = strtol(p, &endptr, 10);
        if (endptr == p) break;
        vec_push(out, (int)v);
        p = endptr;
    }
}
int main(void) {
    static char buf[MAX_LEN];
    Vec nums;
    vec_init(&nums);
    /* 读第一行 */
    safe_getline(buf, sizeof(buf));
    parse_csv(buf, &nums);
    /* 读第二行 */
    safe_getline(buf, sizeof(buf));
    int t = atoi(buf);
    /* 二分查找 */
    int idx = binary_search(nums.data, nums.size, t);
    /* 输出 */
    printf("%d\n", idx);
    vec_free(&nums);
    return 0;
}
不怎么长
所以我整个身体痒痒——emmm我把头文件改成了...
#include <algorithm>
#include <any>
#include <array>
#include <atomic>
#include <barrier>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <charconv>
#include <chrono>
#include <cinttypes>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <codecvt>
#include <compare>
#include <complex>
#include <concepts>
#include <condition_variable>
#include <coroutine>
#include <deque>
#include <exception>
#include <execution>
#include <filesystem>
#include <format>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <latch>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <memory_resource>
#include <mutex>
#include <new>
#include <numbers>
#include <numeric>
#include <optional>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <semaphore>
#include <set>
#include <shared_mutex>
#include <source_location>
#include <span>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stop_token>
#include <streambuf>
#include <string>
#include <string_view>
#include <strstream>
#include <syncstream>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <variant>
#include <vector>
#include <version>
#define MAX_LEN 1000000
/* 安全读取一整行 */
int safe_getline(char *buf, int sz) {
    if (!fgets(buf, sz, stdin)) return -1;
    int len = strlen(buf);
    if (len && buf[len - 1] == '\n') buf[--len] = '\0';
    return len;
}
/* 手工实现一个动态数组 */
typedef struct {
    int *data;
    size_t size;
    size_t cap;
} Vec;
void vec_init(Vec *v) {
    v->cap = 8;
    v->size = 0;
    v->data = (int *)malloc(sizeof(int) * v->cap);
}
void vec_push(Vec *v, int val) {
    if (v->size == v->cap) {
        v->cap <<= 1;
        v->data = (int *)realloc(v->data, sizeof(int) * v->cap);
    }
    v->data[v->size++] = val;
}
void vec_free(Vec *v) {
    free(v->data);
    v->data = NULL;
    v->size = v->cap = 0;
}
/* 手工实现二分查找 */
int binary_search(const int *a, size_t n, int key) {
    size_t l = 0, r = n;
    while (l < r) {
        size_t m = l + ((r - l) >> 1);
        if (a[m] < key) l = m + 1;
        else r = m;
    }
    return (l < n && a[l] == key) ? (int)l : -1;
}
/* 把字符串解析成整数序列 */
void parse_csv(const char *s, Vec *out) {
    const char *p = s;
    while (*p) {
        while (isspace((unsigned char)*p)) ++p;
        if (*p == ',') { ++p; continue; }
        char *endptr;
        long v = strtol(p, &endptr, 10);
        if (endptr == p) break;
        vec_push(out, (int)v);
        p = endptr;
    }
}
int main(void) {
    static char buf[MAX_LEN];
    Vec nums;
    vec_init(&nums);
    /* 读第一行 */
    safe_getline(buf, sizeof(buf));
    parse_csv(buf, &nums);
    /* 读第二行 */
    safe_getline(buf, sizeof(buf));
    int t = atoi(buf);
    /* 二分查找 */
    int idx = binary_search(nums.data, nums.size, t);
    /* 输出 */
    printf("%d\n", idx);
    vec_free(&nums);
    return 0;
}
现在够长了
点个关注和爱心吧
关注我!!!
全部评论 2
建议大家用第一个
2025-10-07 来自 广东
0abab
2025-10-07 来自 广东
0



有帮助,赞一个