课评: 转出学生归档(王子墨/张立坤/黄俊博)+ 周四班级总结 + 新生章梓皓档案 + C++示例代码
This commit is contained in:
53
C++代码/GESP考级/三级/202609公共二进制位.cpp
Normal file
53
C++代码/GESP考级/三级/202609公共二进制位.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define endl '\n'
|
||||
using namespace std;
|
||||
|
||||
int n, num;
|
||||
int s[100];
|
||||
|
||||
string str = "01";
|
||||
|
||||
// 10进制转换成 2进制
|
||||
string f(int n){
|
||||
if(n == 0) return "0";
|
||||
string re = "";
|
||||
while(n > 0){
|
||||
int g = n % 2;
|
||||
|
||||
re = str[g] + re;
|
||||
|
||||
n /= 2;
|
||||
}
|
||||
return re;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
cin >> n;
|
||||
int maxx = 0;
|
||||
for(int i = 1; i <= n; i++){
|
||||
cin >> num;
|
||||
string curr = f(num);
|
||||
int len = curr.size();
|
||||
int idx = 0;
|
||||
for(int j = len - 1; j >= 0; j--){
|
||||
char c = curr[j];
|
||||
if(c == '1'){
|
||||
s[idx]++;
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
maxx = max(maxx, idx); // 统计最长字符串
|
||||
// cout << "当前这个数二进制是" << curr << " " << idx << endl;
|
||||
}
|
||||
|
||||
int cnt = 0;
|
||||
for(int i = 0; i < maxx; i++){
|
||||
if(s[i] == n) {
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
cout << cnt;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user