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

 


分割线:2019.5.7

省赛倒计时4天

今天中石油的比赛拉的是去年青岛网络赛的题;

时隔一年,代码风格变了不少

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define mem(a,b) memset(a,b,sizeof(a))
 4 const int maxn=1e5+50;
 5  
 6 int n;
 7 int b[40];
 8  
 9 int main()
10 {
11     int test;
12     while(~scanf("%d",&test))
13     {
14         while(test--)
15         {
16             scanf("%d",&n);
17             mem(b,0);
18             for(int i=1;i <= n;++i)
19             {
20                 int a;
21                 scanf("%d",&a);
22                 b[(int)log2(a)]++;
23             }
24             printf("%d\n",*max_element(b,b+40));
25         }
26     }
27     return 0;
28 }
View Code

 

posted @ 2018-09-18 21:57  HHHyacinth  阅读(179)  评论(0编辑  收藏  举报