我的二分法一直過不了第二測資點
第 2 測資點(20%): WA (line:21604)
答案不正確您的答案為: 0
正確答案為: 15000
能幫我看看嗎@@?
/**********************************************************************************/
/* Problem: d732 "二分搜尋法" from */
/* Language: C (1816 Bytes) */
/* Result: NA(score:80) judge by this@ZeroJudge */
/* Author: mob5566 at 2012-02-02 22:52:16 */
/**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int n, k, *list, *test, current, i, end, flag, start;
while( scanf( "%d%d", &n, &k ) != EOF )
{
list = malloc( (n+1) * sizeof(int) );
test = malloc( (k+1) * sizeof(int) );
for( i = 1; i <= n; i++ )
{
scanf( "%d", (list+i) );
}
for( i = 1; i <= k; i++ )
{
scanf( "%d", (test+i) );
}
for( i = 1; i <= k; i++ )
{
end = n;
start = 1;
if( (end-(start-1)) % 2 == 0 )
current = (start-1) + (end-(start-1)) / 2;
else
current = (start-1) + (end-(start-1)+1) / 2;
flag = 0;
while( *(test+i) != *(list+current) && (end - start + 1) > 0 )
{
if( *(test+i) > *(list+current) )
{
start = current+1;
if( (end-(start-1)) % 2 == 0 )
current = (start-1) + (end-(start-1)) / 2;
else
current = (start-1) + (end-(start-1)+1) / 2;
}
else
{
end = current-1;
if( end % 2 == 0 )
current = (start-1) + (end-(start-1)) / 2;
else
current = (start-1) + (end-(start-1)+1) / 2;
}
if( *(test+i) == *(list+current) )
flag = 1;
}
if( flag == 1 )
printf( "%d\n", current );
else
printf( "0\n" );
}
free(list);
free(test);
}
return 0;
}