第7章-查找及应用(2020.04.27)

第7章-查找及应用

1. 二分查找的实现【中等】

(二分查找的实现)请尝试用你实现的顺序存储List实现二分查找。List中的Record包含key和other部分。其中key为英文单词,other为单词的中文解释。
【输入】
第一行,查询目标target(英文单词)
第二行,若干条包含key(string)和other(string)的序列,序列按照key的升序排列;(单词数量小于2000)
【输出】查询目标所在的下标,查询目标的内容(key和other),若单词不存在则输出-1即可。
例如
【输入】
wait
computer 电脑 eye 眼睛 hello 你好 train 火车 wait 等待 zebra 斑马
【输出】
4
wait 等待
【输入】
apple
computer 电脑 eye 眼睛 hello 你好 train 火车 wait 等待 zebra 斑马
【输出】
-1 //不存在

#include <iostream>
#include <string>
using namespace std;
enum Error_code { underflow, overflow, range_Error, success, not_present };
const int max_list = 2000;

class Record
{
public:
    Record();
    Record(string x, string y);
    string the_key()const;
    string the_other()const;
private:
    string key;
    string other;
};
typedef Record List_entry;

Record::Record()
{

}

Record::Record(string x, string y)
{
    key = x;
    other = y;
}

string Record::the_key()const
{
    return key;
}

string Record::the_other()const
{
    return other;
}

class Key
{
    string key;
public:
    Key(string x);
    Key(const Record& r);
    string the_key()const;
};

Key::Key(string x)
{
    key = x;
}

Key::Key(const Record& r)
{
    key = r.the_key();
}

string Key::the_key()const
{
    return key;
}

bool operator==(const Key& x, const Key& y)
{
    return x.the_key() == y.the_key();
}

//list的顺序实现
class List
{
public:
    List();
    int size()const;
    bool full()const;
    bool empty()const;
    void clear();
    Error_code insert(int position, const List_entry& x);
    Error_code remove(int position, List_entry& x);
    Error_code retrieve(int position, List_entry& x)const;
    Error_code replace(int position, List_entry& x);
    void traverse(void(*visit)(List_entry& x));
protected:
    int count;
    List_entry entry[max_list];
};

List::List()
{
    count = 0;
}

int List::size()const
{
    return count;
}

bool List::full()const
{
    if (count == max_list) return true;
    return false;
}

bool List::empty()const
{
    if (count == 0) return true;
    return false;
}

void List::clear()
{
    count = 0;
}

Error_code List::retrieve(int position, List_entry& x)const
{
    if (position < 0 || position >= count) return range_Error;
    x = entry[position];
    return success;
}

Error_code List::insert(int position, const List_entry& x)
{
    if (full()) return overflow;
    if (position<0 || position>count) return range_Error;
    for (int i = count - 1; i >= position; i--) entry[i + 1] = entry[i];
    entry[position] = x;
    count++;
    return success;
}

Error_code List::remove(int position, List_entry& x)
{
    if (empty()) return underflow;
    if (position < 0 || position >= count) return range_Error;
    x = entry[position];
    for (int i = position; i < count - 1; i++) entry[i] = entry[i + 1];
    count--;
    return success;
}

Error_code List::replace(int position, List_entry& x)
{
    if (position < 0 || position >= count) return range_Error;
    entry[position] = x;
    return success;
}

void List::traverse(void(*visit)(List_entry& x))
{
    for (int i = 0; i < count; i++)
        (*visit)(entry[i]);
}

Error_code binary_search_1(const List& the_list, const Key& target, int& position, string& find)
{
    Record data;
    int bottom = 0, top = the_list.size() - 1;
    while (bottom < top) {
        int mid = (bottom + top) / 2;
        the_list.retrieve(mid, data);
        if (data.the_key() < target.the_key())
            bottom = mid + 1;
        else top = mid;
    }
    if (top < bottom) return not_present;
    else {
        position = bottom;
        the_list.retrieve(bottom, data);
        if (data.the_key() == target.the_key()) {
            find = data.the_other();
            return success;
        }
        else return not_present;
    }
}

int main()
{
    List a;
    string key, other, tran;
    int i = 0, position;
    cin >> key;
    Key target(key);
    while (cin >> key >> other) {
        Record temp(key, other);
        a.insert(i++, temp);
    }
    Error_code outcome = binary_search_1(a, target, position, tran);
    if (outcome == not_present) {
        cout << -1;
    }
    else{
        cout << position << endl << target.the_key() << ' ' << tran;
    }
    return 0;
}
2. Start and End Position【中等】

(Start and end position)Given an array of integers numbers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1].
【输入】
第一行,要查找的目标值
第二行,值按升序排列的整数数组,数组以-1结束(数组长度小于2000)
【输出】 目标值在数组里,第一次以及最后一次出现的下标
例如:
【输入】
8
5 7 7 8 8 10 -1
【输出】
[3,4]
【输入】
6
5 7 7 8 8 10 -1
【输出】
[-1,-1]

#include <iostream>
using namespace std;

void find_first(int arr[], int target, int len)
{
    int left = 0;
    int right = len - 1;
    while (left <= right) {
        int mid = (left + right) / 2;
        if (arr[mid] >= target) {
            right = mid - 1;
        }
        else {
            left = mid + 1;
        }
    }
    //不要加&& right > -1
    //因为出循环的时候如果找到的是第一个数那么right一定为-1
    if (left < len && arr[left] == target)
        cout << '[' << left << ',';
    else cout << '[' << -1 << ',';
}

void find_last(int arr[], int target, int len)
{
    int left = 0;
    int right = len - 1;
    while (left <= right) {
        int mid = (left + right) / 2;
        if (arr[mid] <= target) {
            left = mid + 1;
        }
        else {
            right = mid - 1;
        }
    }
    //不要加left < len &&
    //同理如果找到的是最后一个数那么出循环的时候left一定为len
    if (right > -1 && arr[right] == target)
        cout << right << ']' << endl;
    else cout << -1 << ']' << endl;
}

int main()
{  
    int target, num, i = 0;
    int arr[2001];
    cin >> target >> num;
    while (num != -1) {
        arr[i++] = num;
        cin >> num;
    }
    find_first(arr, target, i);
    find_last(arr, target, i);
}
3. 求开方值【中等】

(Sqrt function)Implement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
【输入】x的值,x为整数
【输出】x的平方根(整数)
例如:
【输入】 4
【输出】 2
【输入】 8
【输出】 2 //8的开方为2.82842,结果只需要输出整数,故输出2即可。

#include <iostream>
using namespace std;

long long sqrt(int x) {
    long long left = 0;
    long long right = x;
    while (left <= right) {
        long long mid = (left + right) / 2;
        if (mid * mid < x)
            left = mid + 1;
        else if (mid * mid > x)
            right = mid - 1;
        else
            return mid;
    }
    return right;
}

int main()
{  
    long long num;
    cin >> num;
    cout << sqrt(num);
    return 0;
}
4. 有序表合并 【中等】

(有序表合并)给定有序的单链表La和Lb(元素类型为整型),请编写程序将La和Lb合并成为一个新的有序表。(注意:你的程序使用的辅助空间为常数,即若La中有m个节点,Lb中有n个节点,程序需要的节点空间为m+n+常数)
【输入】
单链表La的值(升序)(-1结束)
单链表Lb的值(升序)(-1结束)
【输出】
La和Lb排序的后的结果
例如:
【输入】
1 6 12 19 -1
3 7 16 29 45 99 -1
【输出】
1 3 6 7 12 16 19 29 45 99
(这题就是把链表串起来)

#include <iostream>
using namespace std;
typedef int Node_entry;

struct Node
{
    Node_entry entry;
    Node* next;
    Node();
    Node(Node_entry item, Node* add_on = NULL);
}*LinkList;

Node::Node()
{
    next = NULL;
}

Node::Node(Node_entry item, Node* add_on)
{
    entry = item;
    next = add_on;
}

Node* creat()
{
    Node* head = new Node();
    Node* cur = head;
    int num;
    cin >> num;
    while (num != -1) {
        Node* newptr = new Node(num);
        cur->next = newptr;
        cur = newptr;
        cin >> num;
    }
    return head;
}

Node* hebin(Node* La, Node* Lb)
{
    //原链表中的游走指针
    Node* pa = La->next;
    Node* pb = Lb->next;
    //新的链表中的游走指针
    Node* cur = La;
    cur->next = NULL;
    while (pa && pb) {
        if (pa->entry < pb->entry) {
            cur->next = pa;
            cur = pa;
            pa = pa->next;
        }
        else {
            cur->next = pb;
            cur = pb;
            pb = pb->next;
        }
    }
    if (pa) {
        cur->next = pa;
    }
    if (pb) {
        cur->next = pb;
    }
    return La;
}

void print(Node* L)
{
    Node* cur = L->next;
    while (cur != NULL) {
        cout << cur->entry << ' ';
        cur = cur->next;
    }
}

int main()
{
    Node* La, * Lb, * Lc;
    La = creat();
    Lb = creat();
    Lc = hebin(La, Lb);
    print(Lc);
    //删除Lb头节点
    delete Lb;
    //删除Lc(一串全删啦!)
    while (Lc != NULL) {
        Node* temp = Lc;
        Lc = Lc->next;
        delete temp;
    }
    return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以按照以下步骤下载"jesd220e-ufs3.1_2020.jan.pdf"文件: 1. 打开网页浏览器,如Chrome、Firefox等。 2. 在搜索引擎中输入"jesd220e-ufs3.1_2020.jan.pdf下载",然后点击搜索按钮。 3. 在搜索结果中,找到下载文件的来源网站或页面,例如ieee.org。 4. 点击链接进入相关网站或页面。 5. 在网站或页面上查找文件下载链接,通常是以"Download"或"下载"等字样呈现。 6. 点击下载链接,并按照网站或页面的提示进行操作。 7. 根据您的网络速度和文件大小,等待下载完成。 8. 下载完成后,文件将保存在您的电脑或手机的默认下载文件夹中。 9. 打开下载文件夹,找到名为"jesd220e-ufs3.1_2020.jan.pdf"的文件。 10. 双击文件或使用PDF阅读器打开,您就可以查看和使用该文件了。 请注意,根据不同的资源来源和下载方式,以上步骤可能会有所不同。请根据具体情况进行操作。 ### 回答2: jesd220e-ufs3.1_2020.jan.pdf是一份关于最新的UFS(通用闪存存储)标准的规范文档,提供了关于UFS接口和协议的详细说明。 首先,在下载这个文件之前,我们需要找到可信赖的来源。通常,可以从相关的技术网站、UFS标准组织的官方网站或相关的论坛上获取该文件的下载链接。 一旦找到可信赖的来源,我们可以点击下载链接将文件下载到我们的计算机或移动设备上。由于文件扩展名为.pdf,这意味着它是一个PDF文件,我们需要确保我们的设备上安装了适当的PDF阅读器,以便能够打开并阅读该文件。 下载完成后,我们可以打开PDF阅读器,并在其界面上找到“打开文件”或类似的选项,然后选择我们下载的jesd220e-ufs3.1_2020.jan.pdf文件。PDF阅读器将自动加载该文件并显示其内容。 在阅读文档之前,我们可以使用PDF阅读器提供的一些功能来进行自定义设置,如放大、缩小、搜索特定关键词等。然后,我们可以开始阅读文档,了解UFS3.1标准的各个方面,包括物理层规范、传输层协议、命令和指令集等。 通过阅读该规范文档,我们可以更好地理解UFS接口和协议,从而在设计和开发与UFS相关的设备或系统时能够更准确地遵循和应用这些规范。同时,了解最新的UFS标准也有助于我们了解这一领域的技术发展趋势,为未来的创新提供参考和指导。 ### 回答3: jesd220e-ufs3.1_2020.jan.pdf是联盟通信论坛(JESD)发布的关于UFS(通用闪存存储)规范的最新版本。UFS是一种用于内置存储设备,如智能手机,平板电脑和便携式电脑的闪存存储标准。通过该规范,厂商可以设计出更快、更高效且更稳定的闪存存储解决方案。 如果您想要下载这份文档,您可以按照以下步骤进行操作: 1. 在您的网络浏览器中输入 "jesd220e-ufs3.1_2020.jan.pdf下载"。 2. 在搜索结果中,您会找到一些网站提供该文档的下载链接。请谨慎选择可信的下载源,避免下载到恶意软件。 3. 点击您选择的下载链接,可能需要您提供您的电子邮件地址、用户名等信息。 4. 在下载完成后,您可以在您的计算机或移动设备上找到该文件并打开。 请注意,在下载和查看此类文件时,您需要确保使用的软件支持PDF格式。常见的PDF阅读器有Adobe Acrobat Reader或其他免费的PDF阅读器应用程序。 希望以上信息对您有所帮助!如果您有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值