PID 29364

Invading system

1000ms
65536KB
 
This problem will be judged on WHU. Original ID: 1469
64-bit integer IO format: %lld      Java class name: Main
Font Size:  
Type:  
    Eyelids is a student in WHU, at the same time he is a hacker. Shortly after the end of the examination, he was surprised to find he had failed in advanced mathematics, so he decisively invaded School Academic Department website. In the process of invasion, he found that the contents related to the score is an encrypted file, this file consisted by n numbers, after analysis, he find the password of the encrypted file was the number whose binary form has the least 1 among these n numbers. However, as there are too many numbers, Eyelids hopes you can help him decipher the password, to change the score before the results will be finally announced. 

Input

    The first line is an integer T indicates the number of test cases.

    Input contains multiple test cases. 

    For each test case, the first line has a number n (1<=n<=10^5), showing the number of the numbers. The second line contains n integers separated by blank, showing the n numbers in the file (1<=ai<=10^9).
 

Output

    For each set of data, output the case number first, then the key in a line. If there are more than one number are eligible, output the smallest one.
 

Sample Input

1
5
3 2 4 5 6
 

Sample Output

Case 1: 2
 

Source

 
题目大意: 求给出的n个数中,找出化为二进制数状态时含有1个数最少的那个数,如果多个数同时最少,输出最小的那个。
解题思路位运算    计算出每个数1的个数   进行比较   注意:   在多个数同时最少是输出最小的数的处理。
 1 #include<stdio.h>
 2 #include<string.h>
 3 unsigned int data[100010];
 4 int s[100010];   //存放每个数的1的个数
 5 int BitCount(unsigned int n)
 6 {
 7     unsigned int c = 0 ;   //计数器
 8     while (n>0)
 9     {
10         if((n&1)==1)   //当前位为1
11             ++c ;    //计数器+1
12         n >>= 1 ;   //向右移位
13     }
14     return c ;
15 }
16 int main()
17 {
18     int t,n,ans,savei,i,c;
19     while(scanf("%d",&t)!=EOF)
20     {
21         c=0;
22         while(t--)
23         {
24             memset(data,0,sizeof(data));
25             memset(s,0,sizeof(s));
26             scanf("%d",&n);
27             for(i=0;i<n;i++)
28             {
29                 scanf("%d",&data[i]);
30                 s[i]=BitCount(data[i]);
31             }
32             ans=s[0];
33             savei=0;
34             for(i=0;i<n-1;i++)
35             {
36                 if(ans>s[i+1])
37                 {  ans=s[i+1];savei=i+1;  }
38                 else if(ans==s[i+1])   //当两个数的1的个数相等时  取值较小的那个。
39                 {
40                     if(data[savei]>data[i+1])
41                         savei=i+1;
42                 }
43             }
44             printf("Case %d: %d\n",++c,data[savei]);
45         }
46     }
47     return 0;
48 }

         

posted @ 2013-08-24 20:48  Snail。  阅读(228)  评论(0编辑  收藏  举报