[topcoder] EllysNumberGuessing

时间:2024-07-25 16:37:32

http://community.topcoder.com/stat?c=problem_statement&pm=12975

简单题

#include <cstdlib>
#include <vector>
using namespace std; class EllysNumberGuessing {
public:
int getNumber(vector <int> guesses, vector <int> answers) {
const int LIMIT = 1000000000;
int N = guesses.size();
int res = -2; // not find
vector<int> options{ guesses[0] + answers[0], guesses[0] - answers[0]};
for (int i = 0; i < options.size(); i++) {
int x = options[i];
bool valid = (x >= 1 && x <= LIMIT);
for (int j = 1; j < N; j++) {
valid = valid && (abs(guesses[j] - x) == answers[j]);
}
if (valid) {
if (res == -2) {
res = x;
} else {
res = -1;
}
}
}
return res;
} };