創新工場筆試題

來源:互聯網
上載者:User

#include <stdio.h>#include <stdlib.h>void Heapadjust(int *arr, int parent, int cnt){    int child;    int ValParent;    for(ValParent = arr[parent]; 2 * parent + 1 < cnt; parent = child)    {        child = 2 * parent + 1; //父節點的左子節點        //右子節點存在且大於左子節點,則child+1        if(child + 1 < cnt && arr[child] < arr[child + 1])        {            child++;        }        //若子節點大於根節點,則把子節點的值賦給根節點,否則跳出迴圈;跳出迴圈的原因是:        //既然是從最後一個父節點開始往前構大根堆,如果父節點滿足大根堆,那其子節點也滿足大根堆        if(arr[child] > arr[parent])        {            arr[parent] = arr[child];        }        else        {            break;        }        //沒有跳出迴圈說明,父節點與子節點值交換了,那有可能會導致大根堆被破壞,所以要檢查子節點        //是否還是大根堆        arr[child] = ValParent;    }}void Swap(int *a, int *b){    int temp = *a;    *a = *b;    *b = temp;}void HeapSort(int *arr, int cnt){    int iter = 0;    //初始化大根堆,根據大根堆的特徵,可以減少判斷的次數    for(iter = cnt / 2 - 1; iter >= 0; iter--)    {        Heapadjust(arr, iter, cnt);    }    //大根堆的第一個數是最大的,將它與數組最後一個數作交換,每次交換都前移數組的尾數索引    //交換過程中可能會導致大根堆遭破壞,所以要調用Heapadjust(arr, 0 , iter)構建大根堆    //由於前面已經初始化大根堆,所以已經具備大根堆的特徵,不需要再一個一個父節點的慢慢構建    //只需要將Heapadjust的第二個參數設定為0,即大根堆的第一個父節點即可    for(iter = cnt - 1; iter >= 0; iter--)    {        Swap(&arr[0], &arr[iter]);        Heapadjust(arr, 0 , iter);    }}int main(void){    int arr[] = {3, 2, 1, 4, 5 ,7 ,6, 12, 34, 34 ,56};    int len = 11;    HeapSort(arr, len);    int i = 0;    for( i = 0; i < len; i++)    {        printf("%4d",arr[i]);    }    return 0;}


/*   分治法求平方根*/#include <stdio.h>#include <stdlib.h>#define EPSION 0.000001double MySqrt(double des){    double low = EPSION;    double high = EPSION;    double mid = 0.0;    double sqr = 0.0;    high = des > 1.0 ? des : des + 1.0;    do    {        mid = (low + high) / 2.0;        sqr = mid * mid;        if(sqr > des)        {            high = mid;        }        else        {            low = mid;        }    }while((sqr - des) < -EPSION || (sqr - des) > EPSION);    return mid;}int main(void){    double num = 0.0;    do    {        printf("The number :");        scanf("%lf", &num);        if(num < 0)        {            printf("input error! Please input again!\n");        }    }while(num < 0);    printf("Its sqrt is %.4lf\n", MySqrt(num));    return 0;}


矩陣為MatrixA[m][n],要找的數字為element,從第一行的最後一列的數MatrixA[0][n - 1]開始尋找,element比MatrixA[0][n - 1]小則向左尋找,否則向下尋找,直到找到該數時退出。圖示如下:(可能就會有多個element,只需找到其中一個返回是否存在即可)

/*    楊氏矩陣:尋找*/#include <stdio.h>#include <stdlib.h>#define M 4#define N 4int FindElement(int MatrixA[M][N], int element){    int i = 0, j = N - 1;    while(1)    {        while(MatrixA[i][j] >= element)        {            if(MatrixA[i][j] == element) return 1;            j--;            if(j < 0) return 0;        }        while(MatrixA[i][j] <= element)        {            if(MatrixA[i][j] == element) return 1;            i++;            if(i >= M) return 0;        }    }}int main(void){    int MatrixA[M][N] = {{1, 3, 4, 8},                         {3, 5, 6, 9},                         {6, 7, 10, 11},                         {7, 8, 12,15},                        };    int i = 0;    for(i = 1; i <= 15; i++)    {        printf("element: %-3d", i);        switch(FindElement(MatrixA, i))        {            case 0: printf("don't exist!\n"); break;            case 1: printf(" exist!\n"); break;            default: break;        }    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.