TopCoder SRM 606

就写了两道全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;
	}
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值