当前位置:网站首页>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;
}边栏推荐
- Summary of the mistakes in the use of qpainter in QT gobang man-machine game
- 【解决方案】PaddlePaddle 2.x调用静态图模式
- PermissionError: [Errno 13] Permission denied: ‘data.csv‘
- Implementation of redis distributed lock
- Sword finger offer II 80-100 (continuous update)
- Go language notes (4) go common management commands
- UTF encoding and character set in golang
- Advantages of RFID warehouse management system solution
- 看腾讯大老如何做接口自动化测试
- WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
猜你喜欢

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

Sword finger offer II 80-100 (continuous update)

Hwinfo hardware detection tool v7.26 green version

华为ensp模拟器实现通信安全(交换机)

Alibaba testers use UI automated testing to achieve element positioning

Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t

What should I do if my computer sharing printer refuses access

面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧

From automation to digital twins, what can Tupo do?

shp数据制作3DTiles白膜
随机推荐
Pytorch---使用Pytorch实现LinkNet进行语义分割
PS竖排英文和数字文字怎么改变方向(变竖直显示)
LeetCode 7. 整数反转
Browser render page pass
阿里云国际版CDN的优势
Actual combat simulation │ JWT login authentication
Play the music of youth
How does wincc7.5 SP1 find variables and their positions through cross indexing?
admas零件名重复
y56.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(二九)
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
LeetCode 8. 字符串转换整数 (atoi)
网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
企业数字化转型最佳实践案例:基于云的数字化平台系统安全措施简介与参考
Implementation of redis distributed lock
Sword finger offer II 80-100 (continuous update)
Alibaba testers use UI automated testing to achieve element positioning
Advantages of semantic tags and block level inline elements
UTF encoding and character set in golang
nmap扫描