P1092 虫食算 NOIP2002

为了测试stl
30分的暴力写法。。。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 11;
int n;
char plus1[maxn], plus2[maxn], answer[maxn];
int ans[maxn];
//---------------
bool ok() {
	int tmp[maxn];
	for (int i = n-1; i >= 0; --i)
	{
		tmp[i] = ans[plus1[i] - 'A']+ ans[plus2[i] - 'A'];
	}
	for (int i = n-1; i >= 0; --i)
	{
		if(tmp[i] >= n) {
			tmp[i - 1] += tmp[i] / n;
			tmp[i] %= n;
		}
	}
	for (int i = 0; i < n; ++i)
	{
		if(tmp[i] != ans[answer[i]-'A']) return false;
	}
	return true;
}
//---------------
int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> plus1[i];
	for(int i = 0; i < n; i++) cin >> plus2[i];
	for(int i = 0; i < n; i++) cin >> answer[i];
	for (int i = 0; i < n; ++i)
	{
		ans[i] = i;
	}
	sort(ans, ans+n);
	do{
		if(ok()) break;	
	} while(next_permutation(ans, ans+n));
	for (int i = 0; i < n; ++i)
	{
		cout << ans[i] << ' ';
	}
	return 0;
}

posted on 2016-10-29 15:40  蒟蒻konjac  阅读(99)  评论(0编辑  收藏