当前位置:网站首页>2021 CCPC 哈尔滨 I. Power and Zero(二进制 + 思维)
2021 CCPC 哈尔滨 I. Power and Zero(二进制 + 思维)
2022-07-04 20:13:00 【GHOSTANDBREAD】
借鉴于:I. Power and Zero(二进制,思维)_Looy_cai的博客-CSDN博客
思路:
将一串十进制的数转为二进制,然后各个数位相加,这时候从高到低可能不是递增的,所以要进行高位到低位的转换。直到从高到低都是不递减的状态。这时候最后一位,也就是数值最大的那一位,就是答案。
注意:
将一串数转为二进制时,每个数的二进制长度可能不同,所以要记录最大长度作为二进制数组的长度。
从高到低转换时,转换一遍是不能保证达到不递减的状态的,因为bit[1]和bit[2]转换后,bit[2]和bit[3]又转换后,bit[2]变小了,bit[1]又比bit[2]大了,所以要不断循环直到二进制数组达到从高到低都是不递减的状态。
代码:
#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int t, n;
int bit[50];
int main() {
scanf("%d", &t);
while(t --) {
scanf("%d", &n);
int x;
int len = -1;
memset(bit, 0, sizeof bit);
for(int i = 0; i < n; i ++) {
scanf("%d", &x);
int ind = 0;
while(x) {
bit[++ ind] += x % 2;
x /= 2;
}
len = max(len, ind);
}
bool ok = true;
while(ok) {
ok = false;
for(int i = len; i >= 2; i --) {
if(bit[i] > bit[i - 1]) {
ok = true;
int avg = (bit[i] * 2 + bit[i - 1]) / 3;
bit[i - 1] += (bit[i] - avg) * 2;
bit[i] = avg;
}
}
}
printf("%d\n", bit[1]);
}
return 0;
}边栏推荐
- colResizable.js自动调整表格宽度插件
- How does win11 search for wireless displays? Win11 method of finding wireless display device
- 【解决方案】PaddlePaddle 2.x调用静态图模式
- LeetCode 8. String conversion integer (ATOI)
- See how Tencent does interface automation testing
- 偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
- Foxit pdf editor v10.1.8 green version
- Browser render page pass
- Actual combat simulation │ JWT login authentication
- acwing 3302. 表达式求值
猜你喜欢

PS vertical English and digital text how to change direction (vertical display)

数十亿公民信息遭泄漏!公有云上的数据安全还有“救”吗?

【optimtool.unconstrain】无约束优化工具箱

What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system

Explication détaillée du mécanisme de distribution des événements d'entrée multimodes

How to solve the problem that win11 cannot write the value to the registry key?

Sword finger offer II 80-100 (continuous update)

看腾讯大老如何做接口自动化测试

admas零件名重复

Alibaba testers use UI automated testing to achieve element positioning
随机推荐
RFID仓库管理系统解决方案有哪些功能模块
Hands on deep learning (III) -- convolutional neural network CNN
Go notes (1) go language introduction and characteristics
HMS Core 机器学习服务
[micro service SCG] use of predict
word中插入圖片後,圖片上方有一空行,且删除後布局變亂
BFC interview Brief
nmap扫描
网件r7000梅林系统虚拟内存创建失败,提示USB磁盘读写速度不满足要求解决办法,有需要创建虚拟内存吗??
In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
Advantages of semantic tags and block level inline elements
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
Jekins initialization password not found or not found
js 3D爆炸碎片图片切换js特效
colResizable.js自动调整表格宽度插件
admas零件名重复
测试用例 (TC)
LeetCode 7. Integer inversion
GVM use
torch.tensor和torch.Tensor的区别