当前位置:网站首页>码蹄集 - MT3114 · 有趣的平衡 - 用样例通俗地讲解
码蹄集 - MT3114 · 有趣的平衡 - 用样例通俗地讲解
2022-07-01 05:37:00 【Tisfy】
有趣的平衡
时间限制:1秒
空间限制:64M
题目描述
小码哥特别喜欢平衡,有一天小码哥收到了朋友送的跷跷板,当跷跷板两边重量相同时就达到了平衡。正好,他有一些砝码 2 0 , 2 1 ⋯ , 2 ∞ 2^0,2^1\cdots,2^{\infty} 20,21⋯,2∞,每种砝码只有一个,小码哥希望在左右两边都放一些砝码,使得两边的重量差的绝对值为 x x x,两边必须都放砝码,不能不放砝码,请问小码哥可以做到吗,给出你的方法。
输入描述
输入一个整数 x ( 1 ≤ x < 2 30 ) x(1\le x< 2^{30}) x(1≤x<230)
输出描述
输出四行
第一行代表左边法码的数量
第二行列出代表左边砝码所放的砝码的重量
第三行一个数代表右边法码的数量
第四行列出右边的砝码所放的砝码的重量
每一行数字之间用空格分隔
备注:
任意一种方法满足题意都是合法的
样例一
输入
6
输出
1
2
1
8
题目分析
其实我们可以把数 x x x按二进制来看。
例如样例中的 6 = 11 0 ( 2 ) 6=110_{(2)} 6=110(2)
然后我们计算 x x x的二进制共有多少位
例如样例 6 6 6的二进制 110 110 110有 3 3 3位
然后我们把天平的一边放置一个重量为 2 位 数 2^{位数} 2位数的砝码
例如样例 6 6 6的一边放置重量为 2 3 = 8 2^{3}=8 23=8的砝码
然后我们求出另一边需要放置的砝码的重量(记为 r e m a i n remain remain) r e m a i n = 2 位 数 − x remain=2^{位数}-x remain=2位数−x
例如样例 6 6 6的另一边放置重量 r e m a i n remain remain为 8 − 6 = 2 8-6=2 8−6=2
最后把 r e m a i n remain remain写成二进制形式,第 i i i位为 1 1 1的话就放置一个重量为 2 i 2^i 2i的砝码(从第 0 0 0位算起)
例如样例 6 6 6另一边的 2 = 1 0 ( 2 ) 2=10_{(2)} 2=10(2),第 0 0 0位是 0 0 0,第 1 1 1位是 1 1 1,因此就放置一个重量为 2 1 = 2 2^1=2 21=2的砝码。
到此,问题解决。
AC代码
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int main() {
int n;
cin >> n;
int k = 1;
while (k <= n) {
k *= 2;
}
int remain = k - n;
cout << 1 << endl;
cout << k << endl;
vector<int> right;
int k2 = 1;
while (k2 < k) {
if (k2 & remain) {
right.push_back(k2);
}
k2 *= 2;
}
cout << right.size() << endl;
for (int i = 0; i < right.size(); i++) {
cout << right[i] << ' ';
}
puts("");
return 0;
}
每周提前更新菁英班周赛题解,点关注,不迷路
原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125545873
边栏推荐
- Mongodb学习篇:安装后的入门第一课
- Printk debugging summary
- Explanation of characteristics of hydraulic slip ring
- JDBC常见面试题
- Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
- Thread process foundation of JUC
- tese_ Time_ 2h
- 2/15 (awk, awk conditions, awk processing design can perform additional tasks, and use awk array +for loop to realize advanced search)
- busybox生成的东西
- printk 调试总结
猜你喜欢

云原生存储解决方案Rook-Ceph与Rainbond结合的实践

Unity project experience summary

Simple implementation of database connection pool

激活函数简述

Redis database deployment and common commands

Things generated by busybox

基于微信小程序的青少年生理健康知识小助手(免费获取源码+项目介绍+运行介绍+运行截图+论文)

Usage and principle of synchronized

Use and principle of reentrantlock

What is the at instruction set often used in the development of IOT devices?
随机推荐
rust猜数字游戏
el-cascader回显失败;el-cascader回显不出来
Flutter 实现每次进来界面都刷新数据
Data governance: data governance framework (Part I)
Web Security (x) what is OAuth 2.0?
eBPF Cilium实战(2) - 底层网络可观测性
Things generated by busybox
使用 Nocalhost 开发 Rainbond 上的微服务应用
数据治理:元数据管理实施(第四篇)
libpng12.so.0: cannot open shared object file: No such file or directory 亲测有效
Design and application of immutable classes
Use and principle of reentrantlock
3D建模與處理軟件簡介 劉利剛 中國科技大學
Mongodb学习篇:安装后的入门第一课
A little assistant for teenagers' physiological health knowledge based on wechat applet (free source code + project introduction + operation introduction + operation screenshot + Thesis)
College community management system based on boot+jsp (with source code download link)
printk 调试总结
mysql 将毫秒数转为时间字符串
Unity project experience summary
【问题思考总结】为什么寄存器清零是在用户态进行的?