[topcoder] EllysNumberGuessing

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;
	}

};

  

原文地址:https://www.cnblogs.com/lautsie/p/3903156.html