編程珠璣 第四章 程式設計

來源:互聯網
上載者:User

1、迭代實現二分尋找

int b_s1(int t[], int i, int j, int s){    if(s < t[i] || s > t[j])    {        return -1;    }    while(1)    {        if(i > j)        {            return -1;        }             int m = (i + j)/2;        if(t[m] == s)        {            return m;        }        else if(t[m] < s)        {            i = m + 1;        }        else        {            j = m - 1;        }    }}

 

2、遞迴實現二分尋找

 

int b_s(int t[], int i, int j, int s){    if(i > j)    {        return -1;    }    if(s < t[i] || s > t[j])    {        return -1;    }    int m = (i + j)/2;        if(t[m] == s)    {        return m;    }    else if(t[m] < s)    {        i = m + 1;    }    else    {        j = m - 1;    }    return b_s(t, i, j, s);}

 

3、習題之一,二分尋找第一個出現的尋找資料

int b_s2(int t[], int i, int j, int s){    int sch = -1;    if(s < t[i] || s > t[j])    {        return -1;    }    while(1)    {        if(i > j)        {            return sch;        }             int m = (i + j)/2;        if(t[m] == s)        {            sch = m;            j = m - 1;        }        else if(t[m] < s)        {            i = m + 1;        }        else        {            j = m - 1;        }    }}

 

4、完整程式,含測試部分

int b_s(int t[], int i, int j, int s){    if(i > j)    {        return -1;    }    if(s < t[i] || s > t[j])    {        return -1;    }    int m = (i + j)/2;        if(t[m] == s)    {        return m;    }    else if(t[m] < s)    {        i = m + 1;    }    else    {        j = m - 1;    }    return b_s(t, i, j, s);}int b_s1(int t[], int i, int j, int s){    if(s < t[i] || s > t[j])    {        return -1;    }    while(1)    {        if(i > j)        {            return -1;        }             int m = (i + j)/2;        if(t[m] == s)        {            return m;        }        else if(t[m] < s)        {            i = m + 1;        }        else        {            j = m - 1;        }    }}int b_s2(int t[], int i, int j, int s){    int sch = -1;    if(s < t[i] || s > t[j])    {        return -1;    }    while(1)    {        if(i > j)        {            return sch;        }             int m = (i + j)/2;        if(t[m] == s)        {            sch = m;            j = m - 1;        }        else if(t[m] < s)        {            i = m + 1;        }        else        {            j = m - 1;        }    }}int _tmain(int argc, _TCHAR* argv[]){    int t[10] ;        for(int i = 0; i < 10; i++)    {        t[i] = i + 1;    }    int index = b_s(t, 0, 9, 5);    index = b_s(t, 0, 9, 10);    index = b_s(t, 0, 9, 10000);    index = b_s(t, 0, 9, 0);    index = b_s(t, 0, 9, -1);    index = b_s1(t, 0, 9, 5);    index = b_s1(t, 0, 9, 10);    index = b_s1(t, 0, 9, 10000);    index = b_s1(t, 0, 9, 0);    index = b_s1(t, 0, 9, -1);    index = b_s2(t, 0, 9, 5);    index = b_s2(t, 0, 9, 10);    index = b_s2(t, 0, 9, 10000);    index = b_s2(t, 0, 9, 0);    index = b_s2(t, 0, 9, -1);    t[2] = 4;    index = b_s2(t, 0, 9, 4);    for(int i = 1; i < 9; i++)    {        t[i] = 5;    }    index = b_s2(t, 0, 9, 5);return 0;}

 5、第七題
給定一個滿足0<= x <= 1的點(x, y),尋找包圍這個點的兩個線段,前提條件就是0 到 1之間,所有線段不相交,y值遞增。

解題分支:
遞增可以使用二分尋找,直接找最後一個比指定點大的線段,這個線卡要麼在最靠近這個點之上,要麼這個點就在上面。找到後,索引-1,自然就是最靠近的下面的那個線段了。

 

#include<stdio.h> typedef struct point{    int x;    int y;}POINT;/*the up_index must be initialised as -1, or something else below the 0*/void get_approximate_line_up_index(POINT *p,                                int low,                               int high,                               double d_x,                               double d_y,                               int &up_index                               ){    int m = 0;    if(p[high].x * d_x + p[high].y < d_y)    {        return ;    }    if(p[low].x * d_x + p[low].y > d_y)    {        return ;    }            while(1)    {        if(low > high)        {            return;        }        m = (low + high) / 2;        if(p[m].x * d_x + p[m].y < d_y)        {            low = m + 1;        }        else if(p[m].x * d_x + p[m].y > d_y)        {            up_index = m;            high = m - 1;        }        else         {            up_index = m;            return ;        }    }}void print_line_pair(int low, int high, int index){    int m_l = 0;    int m_h = 0;    if(low >= high)    {        printf("invalid input\n");        return;    }    if(index < low || index > high)    {        printf("no match pair\n");        return;    }    if(index == low)    {        m_l = low;        m_h = high;    }    else    {        m_l = index - 1;        m_h = index;    }    printf("macth pair is (low = %d, high = %d)\n", m_l, m_h);}int main() {     POINT p[] = {        { 1, 2},        { 2, 2},        { 3, 2},        { 4, 2},        { 5, 2},        { 6, 2},        { 7, 2},        { 8, 2},        { 9, 2},        { 10, 2},        { 11, 2},    };    int p_size = sizeof p/sizeof p[0];    /*find the (0.5, 3.1)*/    double  x = 0.5;    int     up_index = -1;    for(int index = 0; index < p_size; index++)    {        printf("(x, y) is (%lf, %lf) \n", x, p[index].x * x + p[index].y);    }    printf("\n\n");    get_approximate_line_up_index(p, 0, p_size - 1, x, 3.1, up_index);    print_line_pair(0, p_size - 1, up_index);    up_index = -1;    get_approximate_line_up_index(p, 0, p_size - 1, x, 4.0, up_index);    print_line_pair(0, p_size - 1, up_index);    up_index = -1;    get_approximate_line_up_index(p, 0, p_size - 1, x, 4.1, up_index);    print_line_pair(0, p_size - 1, up_index);    up_index = -1;    get_approximate_line_up_index(p, 0, p_size - 1, x, 4.5, up_index);    print_line_pair(0, p_size - 1, up_index);    up_index = -1;    get_approximate_line_up_index(p, 0, p_size - 1, x, 4.6, up_index);    print_line_pair(0, p_size - 1, up_index);    up_index = -1;    get_approximate_line_up_index(p, 0, p_size - 1, x, 5.1, up_index);    print_line_pair(0, p_size - 1, up_index);    up_index = -1;    get_approximate_line_up_index(p, 0, p_size - 1, x, 5.6, up_index);    print_line_pair(0, p_size - 1, up_index);    up_index = -1;    get_approximate_line_up_index(p, 0, p_size - 1, x, 6.1, up_index);    print_line_pair(0, p_size - 1, up_index);    up_index = -1;    get_approximate_line_up_index(p, 0, p_size - 1, x, 6.6, up_index);    print_line_pair(0, p_size - 1, up_index);    up_index = -1;    get_approximate_line_up_index(p, 0, p_size - 1, x, 7.1, up_index);    print_line_pair(0, p_size - 1, up_index);}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.