[2023 King's Road Data Structure] [Linear Table 11] Complete implementation of C and C++ (can be run directly)

~~~How can the writing style end in a dull way, and the story does not recognize ordinary at the beginning ✌✌✌

If you need the complete code, you can follow the public account below, and reply "code" in the background to get it. Aguang is looking forward to your visit~

topic:
insert image description here

Problem solving ideas:

>分成3种情况,相等、小于、大于
>等于直接返回
>小于、大于分别减半

Code:

#include <iostream>
using namespace std;
#define Maxsize 50

// 定义顺序表结构
typedef struct
{
    int data[Maxsize];
    int length = 0;
} SqList;

// 插入测试数据
void ListInsert(SqList &L)
{
    int val = 0;
    while (cin >> val)
    {
        L.data[L.length++] = val;

        if (cin.get() == '\n')
        {
            break;
        }
    }
}

// 打印顺序表
void PrintList(SqList L)
{
    for (int i = 0; i < L.length; i++)
    {
        cout << L.data[i] << '\t';
    }
    cout << endl;
}

// 题目功能函数
int SearchMid(SqList &L1, SqList &L2, int n)
{
    int s1 = 0, d1 = n - 1, m1, s2 = 0, d2 = n - 1, m2;
    while (s1 != d1 || s2 != d2)
    {
        m1 = (s1 + d1) / 2;
        m2 = (s2 + d2) / 2;

        if (L1.data[m1] == L2.data[m2])
        {
            return L1.data[m1];
        }

        if (L1.data[m1] < L2.data[m2])
        {
            if ((s1 + d1) % 2 == 0)
            {
                s1 = m1;
                d2 = m2;
            }
            else
            {
                s1 = m1 + 1;
                d2 = m2;
            }
        }
        else
        {
            if ((s2 + d2) % 2 == 0)
            {
                d1 = m1;
                s2 = m2;
            }
            else
            {
                d1 = m1;
                s2 = m2 + 1;
            }
        }
    }

    return L1.data[s1] < L2.data[s2] ? L1.data[s1] : L2.data[s2];
}

int main()
{
    SqList L1, L2;  // 创建一个顺序表
    ListInsert(L1); // 插入写测试数据
    ListInsert(L2);

    cout << SearchMid(L1, L2, 5);
}

Guess you like

Origin blog.csdn.net/m0_47256162/article/details/124423798