就写了两道全cha了
EllysSubstringSorter
低级的错误啊...
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <vector>
#define MAXN 1010
using namespace std;
class EllysSubstringSorter {
public:
string getMin(string S, int L) {
string temp = S;
string res = S;
for (int i = 0; i <= temp.length() - L; ++ i) {
sort(temp.begin() + i, temp.begin() + i + L);
if (temp < res)
res = temp;
temp = S;
}
return res;
}
};
EllysNumberGuessing
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <vector>
#define MAXN 1010
using namespace std;
class EllysNumberGuessing {
public:
bool check(int num) {
if (num >= 1 && num <= 1000000000) return true;
return false;
}
int getNumber(vector <int> guesses, vector <int> answers) {
int len = guesses.size();
int a[] = {guesses[0] + answers[0], guesses[0] - answers[0]};
if (!check(a[0])) a[0] = 0;
if (!check(a[1])) a[1] = 0;
if (len <= 1) {
if (a[0] == 0 && a[1] == 0) return -2;
if (a[0] == 0) return a[1];
if (a[1] == 0) return a[0];
return -1;
}
int b[2];
int res = 0;
int i = 1;
bool flag[2] = {true};
for (; i < len; ++ i) {
flag[0] = flag[1] = true;
b[0] = guesses[i] + answers[i];
b[1] = guesses[i] - answers[i];
for (int i = 0; i < 2; ++ i) if (a[i]) {
for (int j = 0; j < 2; ++ j) {
if (a[i] == b[j]) {
res = a[i];
flag[i] = false;
break;
}
}
}
if (flag[0] != flag[1]) break;
}
if (flag[0] && flag[1]) {
return -2;
}
if (!flag[0] && !flag[1]) {
return -1;
}
for (int i = 2; i < len; ++ i) {
b[0] = guesses[i] + answers[i];
b[1] = guesses[i] - answers[i];
if (b[0] != res && b[1] != res) {
return -2;
}
}
return res;
}
};
当时是错在{{42, 42, 42, 42}, {13, 13, 13, 13}}这组数据,标程非常简洁:
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <vector>
#define MAXN 1010
using namespace std;
class EllysNumberGuessing {
public:
int getNumber(vector <int> guesses, vector <int> answers) {
// find the two candidates:
int options[] = { guesses[0] + answers[0], guesses[0] - answers[0] };
int res = -2;
// for each candidate:
for (int k = 0; k < 2; ++ k) {
int x = options[k];
// must be within bounds
bool valid = (1 <= x && x <= 1000000000);
// must obey all the conditions
for (int i = 0; i < guesses.size(); i++) {
valid = valid && (abs(guesses[i] - x) == answers[i] );
if (!valid) break;
}
if (valid) {
if (res != -2) {
res = -1; // found a previous answer, set result to -1
} else {
res = x; // save answer
}
}
}
return res;
}
};