http://ac.jobdu.com/problem.php?pid=1402 特殊的数
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX 1000001
unsigned char hash[MAX/4]; //用两位来保存一个数字,所以这里是MAX/4,而不是MAX/8,如果是用一位来保存一个数字,那么数组的大小只要达到MAX/8就够用了
int main(void)
{
int sum,n,value,min,max,i,index;
while(scanf("%d",&n) != EOF)
{
memset(hash,0,sizeof(hash));
min = 1000001;
max = -1;
while(n--)
{
scanf("%d",&value);
if(value > max)
max = value;
if(value < min)
min = value;
value <<= 1;
index = value >> 3; //确定这个数字保存在某个字符类型上
value %= 8; //确定这个数字保存在某个字符的某个字节位置上
if(hash[index] & (1 << value)) //这个数字已经出现了两次及以上
continue ;
if(hash[index] & (1 << (value+1))) //这个数字已经出现了一次
hash[index] |= (1 << value);
else
hash[index] |= (1 << (value+1)); //这个数字第一次出现
}
sum = 0;
for(i = min;i <= max; i++)
{
index = (i << 1) >> 3;
value = (i << 1) % 8;
if(!(hash[index] & (1 << value)) && (hash[index] & (1 << (value+1))))
{
sum++;
}
}
if(!sum)
{
puts("0");
continue;
}
printf("%d\n",sum);
for(i = min;i <= max; i++)
{
index = (i << 1) >> 3;
value = (i << 1) % 8;
if(!(hash[index] & (1 << value)) && (hash[index] & (1 << (value+1))))
{
printf("%d",i);
break;
}
}
for(i++;i <= max;i++)
{
index = (i << 1) >> 3;
value = (i << 1) % 8;
if(!(hash[index] & (1 << value)) && (hash[index] & (1 << (value+1))))
printf(" %d",i);
}
puts("");
}
}