您的位置:首页 > 其它

HDU 1172 猜数字

2014-05-18 13:12 399 查看
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1172

 

把1000~9999间的数字遍历一遍,如果只有一个符合条件的话,就输出,否则 not sure

 

#include <iostream>
using namespace std;
char data[105][5];
int a[105],b[105];
int main()
{
bool judge(int m,int n);
int n;
int ans;
int count;
int i,j;
bool sign;
while(cin>>n&&n)
{
count=0;
sign=false;
for(i=0;i<n;i++)
{
cin>>data[i]>>a[i]>>b[i];
}
for(i=1000;i<=9999;i++)
{
for(j=0;j<n;j++)
{
sign=judge(i,j);			//判断i是否符合第j个条件
if(!sign)
break;

}
if(sign)
{
count++;
ans=i;
}
}
if(count==1)
cout<<ans<<endl;
else
cout<<"Not sure"<<endl;

}
return 0;
}

bool judge(int m,int n)		//判断m是否符合条件n
{
int i,j,k;
bool mark[4];
char tem[5];
sprintf(tem,"%d",m);
int count=0;
for(i=0;i<4;i++)
mark[i]=false;
for(i=0;i<4;i++)
if(tem[i]==data
[i])
count++;
if(count!=b
)
return false;
count=0;
for (i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(tem[i]==data
[j]&&!mark[j])
{
mark[j]=true;
count++;
break;
}
}

if(count!=a
)
return false;

return true;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM 搜索