Interviews are considered puzzles.
This commit is contained in:
51
puzzles/interviews/tomtom/tomtom_test1.cpp
Normal file
51
puzzles/interviews/tomtom/tomtom_test1.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
|
||||
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
|
||||
VIM: let g:argv=""
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
int solution( std::vector<int> & a )
|
||||
{
|
||||
auto n = a.size();
|
||||
|
||||
int i = 0;
|
||||
for (int c = 0; c < n; ++c ) {
|
||||
auto new_i = i + a[i];
|
||||
if (new_i < 0 || n <= new_i)
|
||||
return i;
|
||||
i = new_i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main ( void )
|
||||
{try{
|
||||
#if 1
|
||||
std::vector<int> a = {
|
||||
2,3,-1,1,-1
|
||||
};
|
||||
|
||||
std::cout << solution( a ) << std::endl;
|
||||
|
||||
#endif
|
||||
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;
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user