九度oj 1402:特殊的数

http://ac.jobdu.com/problem.php?id=1402  用map超时了

View Code
 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<bitset>
5 #include<climits>
6 using namespace std;
7 int main()
8 {
9 bitset<1000008>b1,b2;//放到main外面,编译通不过?
10 int n,x,i;
11 while(scanf("%d",&n)==1)
12 {
13 b1.reset();//b1
14 b2.set();
15 int maxx=INT_MIN;
16 for(i=0;i<n;i++)
17 {
18 scanf("%d",&x);
19 if(maxx<x) maxx=x;
20 if(b1[x]==0) b1[x]=1;//b1[x]为1,代表x在输入流中
21 else b2[x]=0;//b2某位为1,代表输入流中某个数 (出现个数为1 ,或者没出现过 )
22 //出现个数大于1,就把该数对应位置0
23 }
24 int count=0;
25 for(i=1;i<=maxx;i++)
26 if(b1[i] && b2[i]) count++;//找出出现一次数的 个数
27
28 if(count==0) printf("0\n");
29 else
30 {
31 printf("%d\n",count);
32 int num=0;
33 for(i=1;i<=maxx;i++)
34 if(b1[i] && b2[i])//i在输入流,又只出现一次
35 {
36 num++;
37 printf("%d",i);
38 if(num!=count) printf(" ");
39 }
40 printf("\n");
41 }
42 }
43 return 0;
44 }


 

 1 /*每个正数都是一个32位二进制串,只有一个lucky number
2 lucky出现m%n次,其他为n次,如果没有lucky,序列中个位为0的整数和
3 都是n的整数倍,lucky出现m%n次,统计序列中1的整数的个数,假设为C
4 若C%n==0 luck的个位为0,否则C%n一定为m%n,且luck个位为1
5 其它位跟给位一样*/
6 #include<iostream>
7 #include<cstring>
8 #include<cstdio>
9 using namespace std;
10 int a[32];//记录所有整数中,每一位1的个数
11 int main()
12 {
13 int m,n,x,i;
14 while(scanf("%d%d",&n,&m)==2)
15 {
16 memset(a,0,sizeof(a));
17 int num=m;
18 while(num--)
19 {
20 scanf("%d",&x);
21 for(i=0;i<32;i++)
22 {
23 if(x&1) a[i]++;
24 x>>=1; //右移移位
25 }
26 }
27 int temp=m%n;
28 int res=0;
29 for(i=0;i<32;i++)
30 {
31 if(a[i]%n==temp) //lucky num该位的 二进制 位 为 1
32 res |= (1<<i);//
33 }
34 printf("%d\n",res);
35 }
36 return 0;
37 }


 

posted @ 2012-03-28 17:20  keepmoving89  阅读(236)  评论(0编辑  收藏  举报