現在、バイナリ検索を使用しようとしています。何も見つからない場合は、-1を返すことは望ましくありません。今やいくつかの奇妙な理由のために、私のifステートメントは捕まえられていないので、なぜ本当にわからないのです。私が考えている唯一のことは、おそらく範囲ですか?しかし、私は完全には関数のcで動作する方法と私は小さな詳細を忘れているのか分かりません。 hit > list[middle][0]
しかしhit <= list[middle][1]
私の意見では、その場合には、あなたがmiddle
を返すべきであるときループ内の関数をループ内で制御する
int binary_search(int list[][4], int bottom, int top, int hit)
{
printf("bottom: %d top %d \n", bottom,top);
if (bottom > top)
{
if (list[top][0]<= hit && list[top][1] >= hit)
return top;
else if (list[bottom][0] <= hit && list[bottom][1] >= hit)
return bottom;
}
int middle = (top+bottom)/2;
if (hit < list[middle][0])
return binary_search(list, bottom, middle-1, hit);
else if (hit > list[middle][0])
return binary_search(list, middle+1, top, hit);
else if (hit == list[middle][0])
return middle;
}
ループさえありませんか? – harold
'' return = top''?これはコンパイルされず、 "ループ"によって "再帰"を意味しますか?これが私がここに見る唯一の反復的な行動です... –
次善策はどういう意味ですか?確かに機能名は検索していると言っています –