HDU 1172 猜数字

链接: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[n][i])
			count++;
	if(count!=b[n])
		return false;
	count=0;
	for (i=0;i<4;i++)
		for(j=0;j<4;j++)
		{
			if(tem[i]==data[n][j]&&!mark[j])
			{
				mark[j]=true;
				count++;
				break;
			}
		}

	if(count!=a[n])
		return false;
	
	
	return true;
}


 

原文地址:https://www.cnblogs.com/frankM/p/4399527.html