//#define Local
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
#define MAX 2000+10
struct Array
{
int num[MAX];
int len;
};
void change (Array *a, char s[])
{
int i = 0, j = 0;
for (i = 0; i < strlen(s); i++)
{
if (s[i] >= '0' && s[i] <= '9')//有错误过,没写等号
(*a).num[j++] = s[i] - '0';
}
(*a).len = j;
}
int Judge_break(Array a)
{
int i = 0;
for (i = 0; i < a.len; i++)
if (a.num[i] != 0)
return 1;
return 0;
}
int Count_A (Array guess, Array password)
{
int i = 0, count = 0;
for (i = 0; i < password.len; i++)
{
if (guess.num[i] == password.num[i])
{
count++;
}
}
return count;
}
int Count_B (Array guess, Array password)
{
int i = 0, j = 0, count = 0, flag1[MAX], flag2[MAX];//flag标志
memset(flag1, 1, sizeof(flag1));
memset(flag2, 1, sizeof(flag2));
for (i = 0; i < password.len; i++)
{
if (guess.num[i] == password.num[i])
{
flag1[i] = 0;
flag2[i] = 0;
}
}
for (i = 0; i < guess.len; i++)
{
for (j = 0; j < password.len; j++)
{
if (guess.num[i] == password.num[j] && flag1[j] != 0 && flag2[i] != 0)
{
count++;
flag1[j] = 0;
break;
}
}
}
return count;
}
int main()
{
#ifdef Local
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
int n = 0, A = 0, B = 0, count = 0;
char s[MAX];
Array password = {0, 0}, guess = {0, 0};
while (cin >> n && n != 0)
{
cout << "Game " << ++count << ":" <<endl;
getchar();
gets(s);
change(&password, s);//把密码存起来。
while(true)
{
gets(s);
change(&guess, s);//把猜测存起来。
if (Judge_break(guess) == 0) //都是0, 跳出循环。
break;
A = Count_A(guess, password);
B = Count_B(guess, password);
cout << " (" << A << "," << B << ")" << endl;
}
}
}
/*致命错误:
1、题目要求的大小是1000,但是用字符串输入包含了空格所以应该设置为1999以上。
2、freopen不能用fflush。
3、设置标识还错误的原因是例如 答案和猜测的第三个都是5,但是标志可能提前改掉这个的标志。
4、设置两个标志域,感觉比较麻烦,应该有更简单的方法,待优化。
*/
uva - 340 - Master-Mind Hints
最新推荐文章于 2022-09-28 23:22:13 发布