//用BIT高位进位MAP来操作判断,并入排除法
//出现2次的数字为多个时取最小出现数字
#include "stdio.h"
#define MAXNUM 9 //1-9
#define MOVELEN 14 //32-9-9
#define MAXVALUE 511 //2^9-1
int Find_DNum(int *a,int n)
{
int i,temp,map=0,num=0;
for (i=0;i<n;++i)
{
if (map>>MAXNUM==MAXVALUE)
{
temp=~map<<MOVELEN;
if (!(temp&temp-1))
{
temp>>=MOVELEN;
goto RETURN;
}
}
if (map>>(a[i]-1)&1)
{
if (!(map>>(a[i]-1+MAXNUM)&1))
{
map^=1<<(a[i]-1);
map|=1<<(a[i]-1+MAXNUM);
}
}
else
{
map|=1<<(a[i]-1);
}
}
temp=map>>MAXNUM;
for (i=0;i<MAXNUM;++i)
{
if (!(temp&1))
{
map|=1<<i;
}
temp>>=1;
}
temp=~map<<MOVELEN>>MOVELEN;
RETURN:
while (!(temp&1))
{
++num;
temp>>=1;
}
return ++num;
}
int main()
{
int a[]={8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,1,2,3,4,6,7,8,8,9,9};
printf("%d\n",Find_DNum(a,28));
return 0;
}
BIT高位进位MAP排除法
最新推荐文章于 2024-05-29 19:10:43 发布