C語言部落格作業--函數嵌套調用

來源:互聯網
上載者:User

標籤:inf   amp   沒有   define   可讀性   否則   ++   宏定義   調用   

一、實驗作業1.1 十進位轉換二進位設計思路
如果n==0 輸出0 否則如果n==1 輸出1否則    dectobin(n/2)     輸出n%2
代碼

調試問題
  • 二、本周題目集的PTA最後排名

三、閱讀代碼
  • 用遞迴實現二分尋找
    ```

    include

    int r_search(int arr[], int low, int high, int k);
    int main( )
    {
    int key,index,n,i;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++){
    scanf("%d",&a[i]);
    }
    printf("輸入要尋找的關鍵字:\n");
    scanf("%d", &key);
    index = r_search(a, 0, n-1, key);
    if(index >= 0)
    printf("關鍵字所在位置是:%d \n", index);
    else
    printf("沒有找到關鍵字\n");
    return 0;
    }

int r_search(int arr[], int low, int high, int k)
{
int i,mid;
if (low>high) i=-1;
else
{
mid=(low+high)/2;
if(arr[mid]==k)
i=mid;
else if(arr[mid]>k)
i=r_search(arr, low,mid-1,k);
else
i=r_search(arr, mid+1,high,k);
}
return i;
}
```

  • 用遞迴實現二分尋找在形式上更為簡潔,將二分尋找封裝成函數,不同情況下進入不同的遞迴式子,可讀性高。
四、本周學習總結(1.介紹本周學習內容
  • 學會了函數嵌套調用。
  • 遞迴函式中遞迴出口和遞迴式子是要點,掌握式子的關係不要深究過程,不然會混亂。
  • 宏定義格式#define 宏名 宏定義字串

    2.學習體會
  • 工程作業剛開始時都不會建工程,一些函數都不太會利用,刪除插入太會。

C語言部落格作業--函數嵌套調用

聯繫我們

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