2018.9青岛网络预选赛(K)

传送门:Problem K

https://www.cnblogs.com/violet-acmer/p/9664805.html

题意:

  给你n个数,找出满足条件的最多的数的个数。

题解:

  满足条件的数的转化为二进制最高位一定相同,否则,肯定不满足条件,此题就转换为最高位相同的二进制数最多有多少个的问题。

AC代码:

  

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int n;
 8 int res[31];//(1<<31) > 1e9 
 9 
10 void Initial()
11 {
12     scanf("%d",&n);
13     memset(res,0,sizeof(res));
14 }
15 void Process()
16 {
17     while(n--)
18     {
19         int a;
20         scanf("%d",&a);
21         int t=0;
22         while(a > 0)
23         {
24             t++;
25             a=a>>1;//向左位移最多的次数便是当前数转化成二进制后的位数
26         }
27         res[t]++;//位数相同的数最高位都是1
28     }
29     printf("%d\n",*max_element(res+0,res+31));//输出最高位相同的最多的数
30 }
31 
32 int main()
33 {
34     int T;
35     scanf("%d",&T);
36     while(T--)
37     {
38         Initial();
39         Process();
40     }
41     return 0;
42 }
View Code

猜你喜欢

转载自www.cnblogs.com/violet-acmer/p/9671703.html