Professional Documents
Culture Documents
Books Thief BinarySearch
Books Thief BinarySearch
Books Thief BinarySearch
Using this interesting puzzle as our starting point, the problem you are asked to solve now is: Given a positive
integer s (1 ≤ s ≤ 108 ) representing the result obtained by Anita, find out the number of the forgotten page and
the total number of pages in the book.
Input specification:
The input may contain several test cases. Each test case is presented on a single line, and contains one positive
integer s. The input ends with a test case in which s is zero, and this case must not be processed.
Output specification:
For each test case, your program must print two positive integers, separated by a space, denoting the number of
the forgotten page and the total number pages in the book. Each valid test case must generate just one output
line.
Example input:
1
2
3
4
5
6
9000
499977
49999775
0
1 https://www.hackerrank.com/contests/utp-open-2018/challenges/the-book-thief-1
1
Example output:
2 2
1 2
3 3
2 3
1 3
4 4
45 134
523 1000
5225 10000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* El valor devuelto por la funci ó n es un entero c or re s po nd i en te al indice del elemento */
/* que coincide con el valor buscado . Si el valor buscado no se encuentra , entonces el */
/* valor devuelto es : - ( punto de inserci ó n ) - 1. El valor del punto de inserci ó n es el */
/* indice del elemento del arreglo donde deberia encontrarse el valor buscado . */
/* La expresion : " - ( punto de inserci ó n ) - 1" garantiza que el indice devuelto sera */
/* mayor o igual que cero solo si el valor buscado es encontrado . */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
return result ;
}
int main ()
{
int TN [14146] , s , index , i ;
TN [0] = 0;
for ( i = 1; i <= 14142; i ++)
TN [ i ] = TN [ i - 1] + i ;
2
if ( index >= 0)
printf ( " %d %d \ n " , index + 1 , index + 1) ;
else
{
index += 1;
index *= -1;
printf ( " %d %d \ n " , TN [ index ] - s , index ) ;
}
}
return 0;
}
3
Asistentes a clase