hdu 1172 猜数字
2012-01-06 22:26
381 查看
开始看题, 感觉这题要根据输入的那几个数来扩展, 判断; 再细想, 感觉很纠结; 然后呢,再一想, 其搜索范围只有1000-9999因此枚举足够;
思路: 枚举1000-9999的数, 判断其中满足条件的有几个; 如果有且仅有一个, 则可判断; 如果为0或多于1个, 则不能确定
#include<iostream>
#include<cstring>
using namespace std;
struct Node
{
int b;
int c;
int digit[4];
}node[110];
int digit[4];
bool flag[4];
int ans;
int true_count;
int pos_count;
int get_true_number(int digit[4], Node temp)//得到猜对数字 的个数
{
int i, j, count=0;
memset(flag, 0, sizeof(flag));
for(i=0; i<4; i++)
for(j=0; j<4; j++)
{
if( flag[j]!=1 && digit[i]==temp.digit[j] )
{
count++;
flag[j]=1;
break;
}
}
return count;
}
int get_pos_number(int digit[4], Node temp)//得到在正确位置上的个数
{
int i, count=0;
for(i=0; i<4; i++)
{
if( digit[i]==temp.digit[i])
count++;
}
return count;
}
bool Judge( int n )
{
int i, j, k, l, temp, count=0;
memset(digit, 0, sizeof(digit));
for(i=1000; i<=9999; i++)//遍历1000-9999, 如果发现只有一个数符合条件, 说明可以唯一确定
{
j=0; temp=10;
l=i;
while( l>0 )
{
digit[j++]=l%temp;
l/=temp;
}
for(k=0; k<n; k++)
{
true_count=get_true_number(digit, node[k]);//猜对的数 的个数
pos_count=get_pos_number(digit, node[k]);//在正确位置上的个数
if( !( true_count==node[k].b && pos_count==node[k].c ))//如果有条件不符合, 结束
break;
}
if( k>=n )//条件都符合
{
if( count==0 )
{
count++;
ans=i;
}
else return 0;
}
}
if( count==1 ) return 1;
else return 0;
}
int main()
{
int n, a, i, j, temp;
while( cin>>n && n)
{
for(i=0; i<n; i++)
{
cin>>a>>node[i].b>>node[i].c;
temp=10; j=0;
while( a>0 )
{
node[i].digit[j++]=a%temp;
a/=temp;
}
}
if( Judge( n ) ) cout<<ans<<endl;
else cout<<"Not sure"<<endl;
}
}
思路: 枚举1000-9999的数, 判断其中满足条件的有几个; 如果有且仅有一个, 则可判断; 如果为0或多于1个, 则不能确定
#include<iostream>
#include<cstring>
using namespace std;
struct Node
{
int b;
int c;
int digit[4];
}node[110];
int digit[4];
bool flag[4];
int ans;
int true_count;
int pos_count;
int get_true_number(int digit[4], Node temp)//得到猜对数字 的个数
{
int i, j, count=0;
memset(flag, 0, sizeof(flag));
for(i=0; i<4; i++)
for(j=0; j<4; j++)
{
if( flag[j]!=1 && digit[i]==temp.digit[j] )
{
count++;
flag[j]=1;
break;
}
}
return count;
}
int get_pos_number(int digit[4], Node temp)//得到在正确位置上的个数
{
int i, count=0;
for(i=0; i<4; i++)
{
if( digit[i]==temp.digit[i])
count++;
}
return count;
}
bool Judge( int n )
{
int i, j, k, l, temp, count=0;
memset(digit, 0, sizeof(digit));
for(i=1000; i<=9999; i++)//遍历1000-9999, 如果发现只有一个数符合条件, 说明可以唯一确定
{
j=0; temp=10;
l=i;
while( l>0 )
{
digit[j++]=l%temp;
l/=temp;
}
for(k=0; k<n; k++)
{
true_count=get_true_number(digit, node[k]);//猜对的数 的个数
pos_count=get_pos_number(digit, node[k]);//在正确位置上的个数
if( !( true_count==node[k].b && pos_count==node[k].c ))//如果有条件不符合, 结束
break;
}
if( k>=n )//条件都符合
{
if( count==0 )
{
count++;
ans=i;
}
else return 0;
}
}
if( count==1 ) return 1;
else return 0;
}
int main()
{
int n, a, i, j, temp;
while( cin>>n && n)
{
for(i=0; i<n; i++)
{
cin>>a>>node[i].b>>node[i].c;
temp=10; j=0;
while( a>0 )
{
node[i].digit[j++]=a%temp;
a/=temp;
}
}
if( Judge( n ) ) cout<<ans<<endl;
else cout<<"Not sure"<<endl;
}
}
相关文章推荐
- HDU 1172 猜数字
- HDU 1172 猜数字
- HDU 1172 猜数字(暴力)
- HDU 1172 猜数字
- HDU 1172 猜数字(暴力枚举)
- HDU 1172 猜数字 水题
- hdu 1172 猜数字
- hdu 1172 猜数字
- HDU 1172 猜数字 暴力枚举
- HDU - 1172 猜数字
- HDU-1172 猜数字
- HDU-1172 猜数字
- HDU1172 猜数字【水题】
- HDU 1172 猜数字 (傻逼暴力题)
- HDU 1172 猜数字
- HDU 1172 猜数字(暴力)
- hdu 1172 猜数字
- hdu - 1172 猜数字 (思维题)
- HDU 1172 猜数字(模拟)
- hdu 1172 猜数字(暴力枚举)