当前位置:网站首页>码蹄集 - 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
边栏推荐
- Brief description of activation function
- What is the at instruction set often used in the development of IOT devices?
- Leetcode top 100 question 2 Add two numbers
- 3D建模與處理軟件簡介 劉利剛 中國科技大學
- 教务管理系统(免费源码获取)
- Basic electrician knowledge 100 questions
- More than one file was found with OS independent path ‘lib/armeabi-v7a/libyuv. so‘.
- 2022.6.30-----leetcode. one thousand one hundred and seventy-five
- Dynamic verification of new form items in El form; El form verifies that the dynamic form V-IF does not take effect;
- Introduction to 3D modeling and processing software Liu Ligang University of science and technology of China
猜你喜欢

el-cascader回显失败;el-cascader回显不出来

OneFlow源码解析:算子签名的自动推断

0xc000007b the application cannot start the solution normally (the pro test is valid)

Typeorm framework

Day 05 - file operation function

Use and principle of AQS related implementation classes

激活函数简述

使用 Nocalhost 开发 Rainbond 上的微服务应用

轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
![[excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc](/img/c8/e3e31ad9ef214d97228cb501dd752f.jpg)
[excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc
随机推荐
busybox生成的东西
How to create a progress bar that changes color according to progress
Deeply understand the underlying implementation principle of countdownlatch in concurrent programming
OneFlow源码解析:算子签名的自动推断
El cascade echo failed; El cascader does not echo
【QT】qt加减乘除之后,保留小数点后两位
JSON data comparer
And search: the suspects (find the number of people related to the nth person)
从MLPerf谈起:如何引领AI加速器的下一波浪潮
[RootersCTF2019]babyWeb
【医学分割】u2net
Learn the customization and testing of fpga---ram IP from the bottom structure
新手在挖财开通证券账户安全吗?
Introduction to 3D modeling and processing software Liu Ligang University of science and technology of China
Brief description of activation function
Mathematical knowledge: finding the number of divisors
Speed regulation and stroke control based on Ti drv8424 driving stepper motor
多表操作-外键级联操作
Web Security (x) what is OAuth 2.0?
Educational administration management system of SSM (free source code)