Facebook Interview Tasks.

This commit is contained in:
Vahagn Khachatryan
2013-11-25 15:06:36 +04:00
parent 13281ad9b9
commit 5f88695e2d
2 changed files with 130 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
VIM-: let g:cppflags=g:Iboost.g:Itbb
VIM-: let g:ldflags=g:Lboost.g:Ltbb.g:tbbmalloc.g:tbbmproxy
VIM-: let g:ldlibpath=g:Bboost.g:Btbb
VIM: let g:argv=""
*/
#include <iostream>
#include <vector>
#include <stdexcept>
int main ( void )
{try{
//
// Read length.
//
long long n;
std::cin >> n;
if ( n < 4 )
throw std::runtime_error( "Input length is not enough." );
//
// Read seq.
//
typedef std::vector<int> arr_type;
arr_type arr(n);
for ( int& i: arr )
std::cin >> i;
//
// Solve it.
//
long long s = 0;
for ( long long i: arr )
s += i;
long long d = arr[1]-arr[0];
if ( d != arr[2]-arr[1] && d != arr[3]-arr[2])
d = arr[2]-arr[1];
long long idx = (n+1)*n/2-(s-n*arr[0])/d;
//
//
//
long long missing = arr[0]+idx*d;
std::cout << missing;
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

View File

@@ -0,0 +1,70 @@
/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
VIM-: let g:cppflags=g:Iboost.g:Itbb
VIM-: let g:ldflags=g:Lboost.g:Ltbb.g:tbbmalloc.g:tbbmproxy
VIM-: let g:ldlibpath=g:Bboost.g:Btbb
VIM: let g:argv=""
*/
#include <iostream>
#include <vector>
#include <stdexcept>
bool find_winner( unsigned n )
{
int z = 0; // amount of zeros
int m = 0; // amount of moves
while ( n )
{
unsigned b = n & 0x1; // get the last bit.
if ( b )
m += z;
else
z++;
n >>= 1;
}
return m % 2;
}
int main ( void )
{try{
//
// Read length.
//
long long n;
std::cin >> n;
//
// Read seq.
//
typedef std::vector<unsigned> arr_type;
arr_type arr(n);
for ( unsigned& i: arr )
std::cin >> i;
//
// Solve it.
//
for ( unsigned i: arr )
{
if ( find_winner( i ) )
std::cout << "First Player" << std::endl;
else
std::cout << "Second Player" << std::endl;
}
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}