已答 0 / 25

APCS 2021/11 A 卷 第 1 題

C

第 1 / 25 題 ・ A 卷 ・ 原題號 1

執行函數時若搜尋的數字 x 不存在時會回傳位置-1,否則就會回傳數字代表他在a陣列中的位置。請問傳入哪組參數會出現錯誤的答案?

int a[8] = { 2, 4, 7, 10, 40 } ;
int binary_search( int a[] , int le , int r, int x) {
    if( le < r ) {
        int mid = ( le + r ) / 2 ;
        if(a[ mid ] == x)
            return mid ;
        else if( x < a[ mid ] )
            return binary_search( a, le, mid, x) ;
        return binary_search( a, mid+1, r, x) ;
    }
    return -1 ;
}

本站識讀題目部分來自吳邦一教授公開之《APCS C 程式識讀 125 題》與《APCS Python 程式識讀 125 題》,經作者同意作為非營利教學使用。來源:吳邦一的 APCS 題解目錄

更多