http://acm.hdu.edu.cn/showproblem.php?pid=1172
题目描述很简单,思路就是直接模拟。对于每组数据的n个条件,分别从1000到9999进行测试,如果满足条件的数只有一个则记录输出,否则就输出Not sure
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
bool check(string num, string a, int b, int c) //检查是否满足要求
{
int numbersnum = 0, posnum = 0;
int t1[10] = {0}, t2[10] = {0};
for (int i = 0; i < 4; ++i)
{
++t1[num[i]-'0'];
++t2[a[i]-'0'];
}
for (int i = 0; i < 10; ++i)
if (t1[i] && t2[i])
numbersnum = numbersnum + (t1[i] < t2[i] ? t1[i] : t2[i]); //统计相同的数字个数
for (int i = 0; i < 4; ++i)
if (num[i] == a[i])
++posnum; //统计相同位置数字的个数
if (numbersnum == b && posnum == c)
return true;
else
return false;
}
string IntToString(int n)
{
char t[10];
string s;
sprintf(t, "%d", n);
s = t;
return s;
}
struct Node{
string a;
int b, c;
};
int main()
{
int n;
while (cin >> n && n)
{
int ans, count = 0;
Node node[n];
for (int i = 0; i < n; ++i)
cin >> node[i].a >> node[i].b >> node[i].c; //将这n个条件存到一个结构体数组里
for (int i = 1000; i <= 9999; ++i)
{
string t = IntToString(i);
bool exist = 1;
string a;
int b, c;
for (int k = 0; k < n; ++k)
if (!check(t, node[k].a, node[k].b, node[k].c)) //不满足n个条件则测试下一个数
{
exist = 0;
break;
}
if (exist)
{
++count;
ans = i;
}
}
if (count == 1)
cout << ans << endl;
else
cout << "Not sure" << endl;
}
return 0;
}